backbone-global.d.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // Type definitions for Backbone 1.0.0
  2. // Project: http://backbonejs.org/
  3. // Definitions by: Boris Yankov <https://github.com/borisyankov/>, Natan Vivo <https://github.com/nvivo/>
  4. // Definitions: https://github.com/borisyankov/DefinitelyTyped
  5. /// <reference path="../jquery/jquery.d.ts" />
  6. declare module Backbone {
  7. interface AddOptions extends Silenceable {
  8. at?: number;
  9. }
  10. interface HistoryOptions extends Silenceable {
  11. pushState?: boolean;
  12. root?: string;
  13. }
  14. interface NavigateOptions {
  15. trigger?: boolean;
  16. replace?: boolean;
  17. }
  18. interface RouterOptions {
  19. routes: any;
  20. }
  21. interface Silenceable {
  22. silent?: boolean;
  23. }
  24. interface Validable {
  25. validate?: boolean;
  26. }
  27. interface Waitable {
  28. wait?: boolean;
  29. }
  30. interface Parseable {
  31. parse?: any;
  32. }
  33. interface PersistenceOptions {
  34. url?: string;
  35. data?: any;
  36. beforeSend?: (jqxhr: JQueryXHR) => void;
  37. success?: (modelOrCollection?: any, response?: any, options?: any) => void;
  38. error?: (modelOrCollection?: any, jqxhr?: JQueryXHR, options?: any) => void;
  39. }
  40. interface ModelSetOptions extends Silenceable, Validable {
  41. }
  42. interface ModelFetchOptions extends PersistenceOptions, ModelSetOptions, Parseable {
  43. }
  44. interface ModelSaveOptions extends Silenceable, Waitable, Validable, Parseable, PersistenceOptions {
  45. patch?: boolean;
  46. }
  47. interface ModelDestroyOptions extends Waitable, PersistenceOptions {
  48. }
  49. interface CollectionFetchOptions extends PersistenceOptions, Parseable {
  50. reset?: boolean;
  51. }
  52. interface ObjectHash {
  53. [key: string]: any;
  54. }
  55. interface RoutesHash {
  56. [routePattern: string]: string | {(...urlParts: string[]): void};
  57. }
  58. interface EventsHash {
  59. [selector: string]: string | {(eventObject: JQueryEventObject): void};
  60. }
  61. class Events {
  62. on(eventName: string, callback?: Function, context?: any): any;
  63. on(eventMap: EventsHash): any;
  64. off(eventName?: string, callback?: Function, context?: any): any;
  65. trigger(eventName: string, ...args: any[]): any;
  66. bind(eventName: string, callback: Function, context?: any): any;
  67. unbind(eventName?: string, callback?: Function, context?: any): any;
  68. once(events: string, callback: Function, context?: any): any;
  69. listenTo(object: any, events: string, callback: Function): any;
  70. listenToOnce(object: any, events: string, callback: Function): any;
  71. stopListening(object?: any, events?: string, callback?: Function): any;
  72. }
  73. class ModelBase extends Events {
  74. url: any;
  75. parse(response: any, options?: any): any;
  76. toJSON(options?: any): any;
  77. sync(...arg: any[]): JQueryXHR;
  78. }
  79. class Model extends ModelBase {
  80. /**
  81. * Do not use, prefer TypeScript's extend functionality.
  82. **/
  83. private static extend(properties: any, classProperties?: any): any;
  84. attributes: any;
  85. changed: any[];
  86. cid: string;
  87. collection: Collection<any>;
  88. /**
  89. * Default attributes for the model. It can be an object hash or a method returning an object hash.
  90. * For assigning an object hash, do it like this: this.defaults = <any>{ attribute: value, ... };
  91. * That works only if you set it in the constructor or the initialize method.
  92. **/
  93. defaults(): ObjectHash;
  94. id: any;
  95. idAttribute: string;
  96. validationError: any;
  97. urlRoot: any;
  98. constructor(attributes?: any, options?: any);
  99. initialize(attributes?: any, options?: any): void;
  100. fetch(options?: ModelFetchOptions): JQueryXHR;
  101. /**
  102. * For strongly-typed access to attributes, use the `get` method only privately in public getter properties.
  103. * @example
  104. * get name(): string {
  105. * return super.get("name");
  106. * }
  107. **/
  108. /*private*/ get(attributeName: string): any;
  109. /**
  110. * For strongly-typed assignment of attributes, use the `set` method only privately in public setter properties.
  111. * @example
  112. * set name(value: string) {
  113. * super.set("name", value);
  114. * }
  115. **/
  116. /*private*/ set(attributeName: string, value: any, options?: ModelSetOptions): Model;
  117. set(obj: any, options?: ModelSetOptions): Model;
  118. change(): any;
  119. changedAttributes(attributes?: any): any[];
  120. clear(options?: Silenceable): any;
  121. clone(): Model;
  122. destroy(options?: ModelDestroyOptions): any;
  123. escape(attribute: string): string;
  124. has(attribute: string): boolean;
  125. hasChanged(attribute?: string): boolean;
  126. isNew(): boolean;
  127. isValid(options?:any): boolean;
  128. previous(attribute: string): any;
  129. previousAttributes(): any[];
  130. save(attributes?: any, options?: ModelSaveOptions): any;
  131. unset(attribute: string, options?: Silenceable): Model;
  132. validate(attributes: any, options?: any): any;
  133. private _validate(attributes: any, options: any): boolean;
  134. // mixins from underscore
  135. keys(): string[];
  136. values(): any[];
  137. pairs(): any[];
  138. invert(): any;
  139. pick(keys: string[]): any;
  140. pick(...keys: string[]): any;
  141. omit(keys: string[]): any;
  142. omit(...keys: string[]): any;
  143. }
  144. class Collection<TModel extends Model> extends ModelBase {
  145. /**
  146. * Do not use, prefer TypeScript's extend functionality.
  147. **/
  148. private static extend(properties: any, classProperties?: any): any;
  149. model: new (...args:any[]) => TModel;
  150. models: TModel[];
  151. length: number;
  152. constructor(models?: TModel[] | Object[], options?: any);
  153. initialize(models?: TModel[] | Object[], options?: any): void;
  154. fetch(options?: CollectionFetchOptions): JQueryXHR;
  155. comparator(element: TModel): number;
  156. comparator(compare: TModel, to?: TModel): number;
  157. add(model: {}|TModel, options?: AddOptions): TModel;
  158. add(models: ({}|TModel)[], options?: AddOptions): TModel[];
  159. at(index: number): TModel;
  160. /**
  161. * Get a model from a collection, specified by an id, a cid, or by passing in a model.
  162. **/
  163. get(id: number|string|Model): TModel;
  164. create(attributes: any, options?: ModelSaveOptions): TModel;
  165. pluck(attribute: string): any[];
  166. push(model: TModel, options?: AddOptions): TModel;
  167. pop(options?: Silenceable): TModel;
  168. remove(model: TModel, options?: Silenceable): TModel;
  169. remove(models: TModel[], options?: Silenceable): TModel[];
  170. reset(models?: TModel[], options?: Silenceable): TModel[];
  171. set(models?: TModel[], options?: Silenceable): TModel[];
  172. shift(options?: Silenceable): TModel;
  173. sort(options?: Silenceable): Collection<TModel>;
  174. unshift(model: TModel, options?: AddOptions): TModel;
  175. where(properties: any): TModel[];
  176. findWhere(properties: any): TModel;
  177. private _prepareModel(attributes?: any, options?: any): any;
  178. private _removeReference(model: TModel): void;
  179. private _onModelEvent(event: string, model: TModel, collection: Collection<TModel>, options: any): void;
  180. // mixins from underscore
  181. all(iterator: (element: TModel, index: number) => boolean, context?: any): boolean;
  182. any(iterator: (element: TModel, index: number) => boolean, context?: any): boolean;
  183. collect(iterator: (element: TModel, index: number, context?: any) => any[], context?: any): any[];
  184. chain(): any;
  185. contains(value: any): boolean;
  186. countBy(iterator: (element: TModel, index: number) => any): _.Dictionary<number>;
  187. countBy(attribute: string): _.Dictionary<number>;
  188. detect(iterator: (item: any) => boolean, context?: any): any; // ???
  189. drop(): TModel;
  190. drop(n: number): TModel[];
  191. each(iterator: (element: TModel, index: number, list?: any) => void, context?: any): any;
  192. every(iterator: (element: TModel, index: number) => boolean, context?: any): boolean;
  193. filter(iterator: (element: TModel, index: number) => boolean, context?: any): TModel[];
  194. find(iterator: (element: TModel, index: number) => boolean, context?: any): TModel;
  195. first(): TModel;
  196. first(n: number): TModel[];
  197. foldl(iterator: (memo: any, element: TModel, index: number) => any, initialMemo: any, context?: any): any;
  198. forEach(iterator: (element: TModel, index: number, list?: any) => void, context?: any): any;
  199. groupBy(iterator: (element: TModel, index: number) => string, context?: any): _.Dictionary<TModel[]>;
  200. groupBy(attribute: string, context?: any): _.Dictionary<TModel[]>;
  201. include(value: any): boolean;
  202. indexOf(element: TModel, isSorted?: boolean): number;
  203. initial(): TModel;
  204. initial(n: number): TModel[];
  205. inject(iterator: (memo: any, element: TModel, index: number) => any, initialMemo: any, context?: any): any;
  206. isEmpty(object: any): boolean;
  207. invoke(methodName: string, args?: any[]): any;
  208. last(): TModel;
  209. last(n: number): TModel[];
  210. lastIndexOf(element: TModel, fromIndex?: number): number;
  211. map(iterator: (element: TModel, index: number, context?: any) => any, context?: any): any[];
  212. max(iterator?: (element: TModel, index: number) => any, context?: any): TModel;
  213. min(iterator?: (element: TModel, index: number) => any, context?: any): TModel;
  214. reduce(iterator: (memo: any, element: TModel, index: number) => any, initialMemo: any, context?: any): any;
  215. select(iterator: any, context?: any): any[];
  216. size(): number;
  217. shuffle(): any[];
  218. slice(min: number, max?: number): TModel[];
  219. some(iterator: (element: TModel, index: number) => boolean, context?: any): boolean;
  220. sortBy(iterator: (element: TModel, index: number) => number, context?: any): TModel[];
  221. sortBy(attribute: string, context?: any): TModel[];
  222. sortedIndex(element: TModel, iterator?: (element: TModel, index: number) => number): number;
  223. reduceRight(iterator: (memo: any, element: TModel, index: number) => any, initialMemo: any, context?: any): any[];
  224. reject(iterator: (element: TModel, index: number) => boolean, context?: any): TModel[];
  225. rest(): TModel;
  226. rest(n: number): TModel[];
  227. tail(): TModel;
  228. tail(n: number): TModel[];
  229. toArray(): any[];
  230. without(...values: any[]): TModel[];
  231. }
  232. class Router extends Events {
  233. /**
  234. * Do not use, prefer TypeScript's extend functionality.
  235. **/
  236. private static extend(properties: any, classProperties?: any): any;
  237. /**
  238. * Routes hash or a method returning the routes hash that maps URLs with parameters to methods on your Router.
  239. * For assigning routes as object hash, do it like this: this.routes = <any>{ "route": callback, ... };
  240. * That works only if you set it in the constructor or the initialize method.
  241. **/
  242. routes: RoutesHash | any;
  243. constructor(options?: RouterOptions);
  244. initialize(options?: RouterOptions): void;
  245. route(route: string|RegExp, name: string, callback?: Function): Router;
  246. navigate(fragment: string, options?: NavigateOptions): Router;
  247. navigate(fragment: string, trigger?: boolean): Router;
  248. private _bindRoutes(): void;
  249. private _routeToRegExp(route: string): RegExp;
  250. private _extractParameters(route: RegExp, fragment: string): string[];
  251. }
  252. var history: History;
  253. class History extends Events {
  254. handlers: any[];
  255. interval: number;
  256. start(options?: HistoryOptions): boolean;
  257. getHash(window?: Window): string;
  258. getFragment(fragment?: string, forcePushState?: boolean): string;
  259. stop(): void;
  260. route(route: string, callback: Function): number;
  261. checkUrl(e?: any): void;
  262. loadUrl(fragmentOverride: string): boolean;
  263. navigate(fragment: string, options?: any): boolean;
  264. static started: boolean;
  265. options: any;
  266. private _updateHash(location: Location, fragment: string, replace: boolean): void;
  267. }
  268. interface ViewOptions<TModel extends Model> {
  269. model?: TModel;
  270. // TODO: quickfix, this can't be fixed easy. The collection does not need to have the same model as the parent view.
  271. collection?: Backbone.Collection<any>; //was: Collection<TModel>;
  272. el?: any;
  273. id?: string;
  274. className?: string;
  275. tagName?: string;
  276. attributes?: {[id: string]: any};
  277. }
  278. class View<TModel extends Model> extends Events {
  279. /**
  280. * Do not use, prefer TypeScript's extend functionality.
  281. **/
  282. private static extend(properties: any, classProperties?: any): any;
  283. constructor(options?: ViewOptions<TModel>);
  284. initialize(options?: ViewOptions<TModel>): void;
  285. /**
  286. * Events hash or a method returning the events hash that maps events/selectors to methods on your View.
  287. * For assigning events as object hash, do it like this: this.events = <any>{ "event:selector": callback, ... };
  288. * That works only if you set it in the constructor or the initialize method.
  289. **/
  290. events(): EventsHash;
  291. $(selector: string): JQuery;
  292. model: TModel;
  293. collection: Collection<TModel>;
  294. //template: (json, options?) => string;
  295. setElement(element: HTMLElement|JQuery, delegate?: boolean): View<TModel>;
  296. id: string;
  297. cid: string;
  298. className: string;
  299. tagName: string;
  300. el: any;
  301. $el: JQuery;
  302. setElement(element: any): View<TModel>;
  303. attributes: any;
  304. $(selector: any): JQuery;
  305. render(): View<TModel>;
  306. remove(): View<TModel>;
  307. make(tagName: any, attributes?: any, content?: any): any;
  308. delegateEvents(events?: EventsHash): any;
  309. delegate(eventName: string, selector: string, listener: Function): View<TModel>;
  310. undelegateEvents(): any;
  311. undelegate(eventName: string, selector?: string, listener?: Function): View<TModel>;
  312. _ensureElement(): void;
  313. }
  314. // SYNC
  315. function sync(method: string, model: Model, options?: JQueryAjaxSettings): any;
  316. function ajax(options?: JQueryAjaxSettings): JQueryXHR;
  317. var emulateHTTP: boolean;
  318. var emulateJSON: boolean;
  319. // Utility
  320. function noConflict(): typeof Backbone;
  321. var $: JQueryStatic;
  322. }
  323. declare module "backbone" {
  324. export = Backbone;
  325. }