Merge pull request #32 from docgit/master

Fix some issues in Backbone and add definitions for Knockback.
This commit is contained in:
Boris Yankov
2012-10-30 16:57:20 -07:00
2 changed files with 342 additions and 131 deletions

View File

@@ -1,160 +1,196 @@
// Type definitions for Backbone 0.9
// Project: http://backbonejs.org/
// Definitions: https://github.com/borisyankov/DefinitelyTyped
// https://github.com/borisyankov/DefinitelyTyped
declare module Backbone {
export class Events {
on(events: string, callback: (event) => any, context?: any): any;
off(events?: string, callback?: (event) => any, context?: any): any;
trigger(events: string, ...args: any[]): any;
export interface AddOptions extends Silenceable {
at: number;
}
export interface CreateOptions extends Silenceable {
wait: bool;
}
export interface Destroyable {
success(model: any, resonse: any);
error(model: any, resonse: any);
}
export class Model extends Events {
export interface HistoryOptions extends Silenceable {
pushState: bool;
root: string;
}
export interface NavigateOptions {
trigger: bool;
}
export interface RouterOptions {
routes: any;
}
export interface Silenceable {
silent: bool;
}
export class Events {
on(eventName: string, callback: (event) => void, context?: any) : any;
bind(eventName: string, callback: (event) => void, context?: any) : any;
off(eventName?: string, callback?: (event) => void, context?: any): any;
unbind(eventName?: string, callback?: (event) => void, context?: any): any;
trigger(eventName: string, ...args: any[]): any;
}
export class ModelBase extends Events {
fetch(options? : Destroyable);
url: string; // or url(): string;
parse(response);
toJSON(): string;
}
export class Model extends ModelBase {
static extend(properties: any, classProperties?: any): any; // do not use, prefer TypeScript's extend functionality
attributes: any;
changed: any[];
cid: string;
defaults : any; // or defaults();
id: any;
idAttribute: string;
urlRoot : string; // or urlRoot()
constructor (attributes?: any, options?: any);
initialize(attributes?: any);
get(attributeName: string): any;
set(attributeName: string, value: any): void;
set(obj: any): void;
set(attributeName: string, value: any);
set(obj: any);
escape(attribute);
has(attribute);
unset(attribute, options? );
clear(options? );
id: any;
idAttribute: any;
cid;
attributes;
changed;
bind(ev: string, f: Function, ctx?: any): void; /// ????
defaults; // or defaults();
toJSON(): string;
fetch(options? );
save(attributes? , options? ): void;
destroy(options? ): void;
validate(attributes);
isValid();
url();
urlRoot; // or urlRoot()
parse(response);
clone();
isNew();
change();
hasChanged(attribute? );
changedAttributes(attributes? );
previous(attribute);
previousAttributes();
changedAttributes(attributes? : any) : any[];
clear(options? : Silenceable );
clone() : Model;
destroy(options? : Destroyable );
escape(attribute : string);
has(attribute : string) : bool;
hasChanged(attribute? : string ) : bool;
isNew() : bool;
isValid() : string;
previous(attribute : string) : any;
previousAttributes(): any[];
save(attributes? : any, options? : Destroyable );
unset(attribute: string, options? : Silenceable );
validate(attributes : any) : any;
}
export class Collection {
export class Collection extends ModelBase {
static extend(properties: any, classProperties?: any): any; // do not use, prefer TypeScript's extend functionality
model;
constructor (models? , options? );
models;
toJSON(): any;
///// start UNDERSCORE 28:
bind(ev: string, f: Function, ctx?: any): void;
model: Model;
models : any;
collection: Model;
create(attrs, opts? ): Collection;
each(f: (elem: any) => void ): void;
last(): any;
last(n: number): any[];
filter(f: (elem: any) => any): Collection;
without(...values: any[]): Collection;
// Underscore bindings
each(object: any, iterator: (value, key, list? ) => void , context?: any): any[];
forEach(object: any, iterator: (value, key, list? ) => void , context?: any): any[];
map(object: any, iterator: (value, key, list? ) => void , context?: any): any[];
reduce(list: any[], iterator: any, memo: (memo: any, element: any, index: number, list: any[]) => any, context?: any): any[];
reduceRight(list: any[], iterator: (memo: any, element: any, index: number, list: any[]) => any, memo: any, context?: any): any[];
find(list: any[], iterator: any, context?: any): any; // ???
detect(list: any[], iterator: any, context?: any): any; // ???
filter(list: any[], iterator: any, context?: any): any[];
select(list: any[], iterator: any, context?: any): any[];
reject(list: any[], iterator: any, context?: any): any[];
every(list: any[], iterator: any, context?: any): bool;
all(list: any[], iterator: any, context?: any): bool;
any(list: any[], iterator?: any, context?: any): bool;
some(list: any[], iterator?: any, context?: any): bool;
contains(list: any, value: any): bool;
contains(list: any[], value: any): bool;
include(list: any, value: any): bool;
include(list: any[], value: any): bool;
invoke(list: any[], methodName: string, arguments: any[]): any;
invoke(object: any, methodName: string, ...arguments: any[]): any;
max(list: any[], iterator?: any, context?: any): any;
min(list: any[], iterator?: any, context?: any): any;
sortBy(list: any[], iterator?: any, context?: any): any;
sortedIndex(list: any[], valueL: any, iterator?: any): number;
toArray(list: any): any[];
size(list: any): number;
first(array: any[], n?: number): any;
initial(array: any[], n?: number): any[];
rest(array: any[], n?: number): any[];
last(array: any[], n?: number): any;
without(array: any[], ...values: any[]): any[];
indexOf(array: any[], value: any, isSorted?: bool): number;
shuffle(list: any[]): any[];
lastIndexOf(array: any[], value: any, fromIndex?: number): number;
isEmpty(object: any): bool;
groupBy(list: any[], iterator: any): any;
add(models, options? );
remove(models, options? );
get(id);
getByCid(cid);
at(index: number);
push(model, options? );
pop(options? );
unshift(model, options? );
shift(options? );
length: number;
//comparator;
sort(options? );
pluck(attribute);
where(attributes);
url; // or url()
parse(response);
fetch(options?: any): void;
reset(models, options? );
create(attributes, options? );
constructor (models? :any , options? );
add(model: Model, options? : AddOptions);
add(models: Model[], options? : AddOptions);
at(index: number) : Model;
comparator(attribute: string): number;
comparator(compare: Model, to:Model): number;
get(id : any) : Model;
getByCid(cid) : Model;
create(attributes: any, options? : CreateOptions ): Collection;
pluck(attribute:string) : any[];
push(model: Model, options? : AddOptions);
pop(options? : Silenceable);
remove(model: Model, options? : Silenceable);
remove(models: Model[], options? : Silenceable);
reset(models : Model[], options? );
shift(options? : Silenceable);
sort(options? : Silenceable);
unshift(model: Model, options?: AddOptions);
where(properies: any): Model[];
all(iterator: (element: Model, index:number) => bool, context?: any): bool;
any(iterator:(element: Model, index:number) => bool, context?: any): bool;
collect(iterator: (element: Model, index:number, context? : any ) => any[] , context?: any): any[];
compact(): Model[];
contains(value: any): bool;
countBy(iterator: (element:Model, index:number) => any) : any[];
countBy(attribute: string) : any[];
detect(iterator: (item: any) => bool, context?: any): any; // ???
difference(...model: Model[]) : Model[];
drop(): Model;
drop(n: number): Model[];
each(iterator: (element: Model, index:number, list? ) => void , context?: any);
every(iterator: (element: Model, index:number) => bool, context?: any): bool;
filter(iterator: (elemebt:Model, index:number) => bool, context?: any): Model[];
find(iterator: (element:Model, index:number) => bool, context?: any): Model;
first(): Model;
first(n: number): Model[];
flatten(shallow?: bool): Model[];
foldl(iterator: (memo: any, element: Model, index:number) => any, initialMemo: any, context?: any): any;
forEach(iterator: (element: Model, index:number, list? ) => void , context?: any);
groupBy(iterator: (element:Model, index:number) => any) : any[];
groupBy(attribute: string) : any[];
include(value: any): bool;
indexOf(element: Model, isSorted?: bool): number;
initial(): Model;
initial(n: number): Model[];
inject(iterator: (memo: any, element: Model, index:number) => any, initialMemo: any, context?: any): any;
intersection(...model: Model[]) : Model[];
isEmpty(object: any): bool;
invoke(methodName: string, arguments?: any[]);
last(): Model;
last(n: number): Model[];
lastIndexOf(element: Model, fromIndex?: number): number;
map(iterator: (element: Model, index:number, context? : any ) => any[] , context?: any): any[];
max(iterator?: (element:Model, index:number) => any, context?: any): Model;
min(iterator?: (element:Model, index:number) => any, context?: any): Model;
object(...values: any[]): any[];
reduce(iterator: (memo: any, element: Model, index:number) => any, initialMemo: any, context?: any): any;
select(iterator: any, context?: any): any[];
size(): number;
shuffle(): any[];
some(iterator:(element: Model, index:number) => bool, context?: any): bool;
sortBy(iterator: (element:Model, index:number) => number, context?: any): Model[];
sortBy(attribute:string, context?: any): Model[];
sortedIndex(element: Model, iterator?: (element:Model, index:number) => number): number;
range(stop: number, step?:number);
range(start: number, stop: number, step?:number);
reduceRight(iterator: (memo: any, element: Model, index: number) => any, initialMemo: any, context?: any): any[];
reject(iterator: (element:Model, index:number) => bool, context?: any): Model[];
rest(): Model;
rest(n: number): Model[];
tail(): Model;
tail(n: number): Model[];
toArray(): any[];
union(...model: Model[]) : Model[];
uniq(isSorted? : bool, iterator?: (element:Model, index:number) => bool) : Model[];
without(...values: any[]): Model[];
zip(...model: Model[]): Model[];
}
export class Router {
static extend(properties: any, classProperties?: any): any; // do not use, prefer TypeScript's extend functionality
routes;
constructor (options? );
route(route, name, callback? );
navigate(fragment, options? );
routes : any;
constructor (options? : RouterOptions);
initialize (options? : RouterOptions);
route(route: string, name: string, callback?: (...parameter:any[]) => void );
navigate(fragment : string, options? : NavigateOptions);
}
export var history: History;
export class History {
start(options? );
start(options? : HistoryOptions );
navigate(fragment: string, options: any);
}
export class Sync {
sync(method, model, options? );
emulateHTTP: bool;
emulateJSONBackbone: bool;
pushSate();
}
export class View {
@@ -166,7 +202,7 @@ declare module Backbone {
$(selector: string): any;
model: Model;
make(tagName: string, attrs? , opts? ): View;
setElement(element: HTMLElement, delegate?: bool): void;
setElement(element: HTMLElement, delegate?: bool);
tagName: string;
events: any;
@@ -176,15 +212,19 @@ declare module Backbone {
attributes;
$(selector);
render();
remove(): void;;
remove();
make(tagName, attributes? , content? );
//delegateEvents: any;
delegateEvents(events?: any): any;
undelegateEvents();
}
export class Utility {
noConflict(): any;
setDomLibrary(jQueryNew);
}
// SYNC
function sync(method, model, options? : Destroyable);
var emulateHTTP: bool;
var emulateJSONBackbone: bool;
// Utility
function noConflict(): Backbone;
function setDomLibrary(jQueryNew);
}

171
Definitions/knockback-0.16.d.ts vendored Normal file
View File

@@ -0,0 +1,171 @@
/// <reference path="backbone-0.9.d.ts" />
/// <reference path="knockout-2.2.d.ts" />
declare module Knockback {
export interface EventWatcherOptions {
emitter : (newEmitter) => void;
update : (newValue) => void;
event_selector : string;
key? : string;
}
export interface FactoryOptions {
factories: any;
}
export interface StoreOptions {
creator: any;
path: string;
store: Store;
factory: Factory;
}
export class Destroyable {
destroy();
}
export class ViewModel extends Destroyable {
constructor (model? : Backbone.Model , options? : ViewModelOptions, viewModel? : ViewModel );
shareOptions() : ViewModelOptions;
extend(source: any);
model(): Backbone.Model;
}
export class EventWatcher extends Destroyable {
useOptionsOrCreate(options, emitter : KnockoutObservableAny, obj : Backbone.Model, callback_options : any);
emitter(): Backbone.Model;
emitter(newEmitter : Backbone.Model);
registerCallbacks(obj: any, callback_info: any);
releaseCallbacks(obj: any);
}
export class Factory {
constructor (parent_factory : any);
useOptionsOrCreate(options: FactoryOptions, obj: any, owner_path: string);
hasPath(path: string): bool;
addPathMapping(path: string, create_info);
addPathMappings(factories: any, owner_path: string);
hasPathMappings(factories: any, owner_path: string) : bool;
creatorForPath(obj: any, path: string);
}
export class Store extends Destroyable {
constructor (model:Backbone.Model, options: StoreOptions);
useOptionsOrCreate(options: StoreOptions, obj: any, observable: KnockoutObservableAny);
clear();
register(obj: Backbone.Model, observable: KnockoutObservableAny, options: StoreOptions);
findOrCreate(obj: Backbone.Model, options: StoreOptions);
}
export class DefaultObservable extends Destroyable {
constructor (targetObservable: KnockoutObservableAny, defaultValue: any);
setToDefault();
}
export class FormattedObservable extends Destroyable {
constructor (format: string, args: any[]);
constructor (format: KnockoutObservableAny, args: any[]);
}
export interface LocalizedObservable {
constructor (value: any, options: any, vm: any);
destroy();
resetToCurrent();
observedValue(value: any);
}
export class TriggeredObservable extends Destroyable {
constructor (emitter: Backbone.ModelBase, event: string);
emitter() : Backbone.ModelBase;
emitter(newEmitter: Backbone.ModelBase);
}
export class Statistics {
constructor ();
clear();
addModelEvent(event: string);
modelEventsStatsString();
register(key: string, obj: any);
unregister(key: string, obj: any);
registeredCount(type: any) : number;
registeredStatsString(success_message: string) : string;
}
export interface OptionsBase {
path?: string; // the path to the value (used to create related observables from the factory).
store?: Store; // a store used to cache and share view models.
factory?: Factory; // a factory used to create view models.
options?: any; // a set of options merge into these options using _.defaults. Useful for extending options when deriving classes rather than merging them by hand.
}
export interface ViewModelOptions extends OptionsBase {
internals?: string[]; // an array of atttributes that should be scoped with an underscore, eg. name -> _name
requires?: string[]; // an array of atttributes that will have kb.Observables created even if they do not exist on the Backbone.Model. Useful for binding Views that require specific observables to exist
keys?: string[]; // restricts the keys used on a model. Useful for reducing the number of kb.Observables created from a limited set of Backbone.Model attributes
if(objOrArray: any); // an array is supplied, excludes keys to exclude on the view model; for example, if you want to provide a custom implementation. If an Object, it provides options to the kb.Observable constructor.
path?: string; // the path to the value (used to create related observables from the factory).
factories?: any; // a map of dot-deliminated paths; for example {'models.name': kb.ViewModel} to either constructors or create functions. Signature: {'some.path': function(object, options)}
}
export interface CollectionOptions extends OptionsBase {
models_only?: bool; // flag for skipping the creation of view models. The collection observable will be populated with (possibly sorted) models.
view_model?: any; // (Constructor) — the view model constructor used for models in the collection. Signature: constructor(model, options)
create?: any; // a function used to create a view model for models in the collection. Signature: create(model, options)
factories?: any; // a map of dot-deliminated paths; for example 'models.owner': kb.ViewModel to either constructors or create functions. Signature: 'some.path': function(object, options)
comparator?: any; //a function that is used to sort an object. Signature: function(model_a, model_b) returns negative value for ascending, 0 for equal, and positive for descending
sort_attribute?: string; // the name of an attribute. Default: resort on all changes to a model.
filters?: any; // filters can be individual ids (observable or simple) or arrays of ids, functions, or arrays of functions.
}
export interface CollectionObservable extends KnockoutObservableArray {
collection() : Backbone.Collection;
destroy();
shareOptions() : CollectionOptions;
filters(filter: any);
comparator(comparatorFunction: any);
sortAttribute(attr: string);
viewModelByModel(model: Backbone.Model) : ViewModel;
hasViewModels(): bool;
}
export interface Utils {
wrappedObservable(obj: any) : any;
wrappedObservable(obj: any, value: any);
wrappedObject(obj: any) : any;
wrappedObject(obj: any, value: any);
wrappedModel(obj: any) : any;
wrappedModel(obj: any, value: any);
wrappedStore(obj: any) : any;
wrappedStore(obj: any, value: any);
wrappedFactory(obj: any) : any;
wrappedFactory(obj: any, value: any);
wrappedEventWatcher(obj: any) : any;
wrappedEventWatcher(obj: any, value: any);
wrappedDestroy(obj: any);
valueType(observable: KnockoutObservableAny) : any;
pathJoin(path1: string, path2: string): string;
optionsPathJoin(options: any, path: string): any;
inferCreator(value: any, factory: Factory, path: string, owner: any, key: string);
createFromDefaultCreator(obj: any, options?: any);
hasModelSignature(obj: any): bool;
hasCollectionSignature(obj: any): bool;
}
export interface Static extends Utils {
collectionObservable(model?: Backbone.Collection, options?: CollectionOptions): CollectionObservable;
observable(model?: Backbone.Model, options?: any) : KnockoutObservableAny;
viewModel(model?: Backbone.Model, options?: any) : KnockoutObservableAny;
defaultObservable(targetObservable: KnockoutObservableAny, defaultValue: any) : KnockoutObservableAny;
formattedObservable(format: string, args: any[]) : KnockoutObservableAny;
formattedObservable(format: KnockoutObservableAny, args: any[]): KnockoutObservableAny;
localizedObservable(data: any, options: any): KnockoutObservableAny;
release(object: any, pre_release?: () => void );
releaseKeys(object: any);
releaseOnNodeRemove(viewmodel: ViewModel, node: Element);
renderTemplate(template: string, viewModel: ViewModel, options: any);
renderAutoReleasedTemplate(template: string, viewModel: ViewModel, options: any);
applyBindings(viewModel: ViewModel, node?: Element);
}
};
declare var kb: Knockback.Static;