/// /// export = Backbone; export as namespace Backbone; declare namespace Backbone { type _Omit = Pick>; type _Result = T | (() => T); type _StringKey = keyof T & string; interface AddOptions extends Silenceable { at?: number | undefined; merge?: boolean | undefined; sort?: boolean | undefined; } interface CollectionSetOptions extends Parseable, Silenceable { add?: boolean | undefined; remove?: boolean | undefined; merge?: boolean | undefined; at?: number | undefined; sort?: boolean | undefined; } interface HistoryOptions extends Silenceable { pushState?: boolean | undefined; root?: string | undefined; hashChange?: boolean | undefined; } interface NavigateOptions { trigger?: boolean | undefined; replace?: boolean | undefined; } interface RouterOptions { routes: _Result; } interface Silenceable { silent?: boolean | undefined; } interface Validable { validate?: boolean | undefined; } interface Waitable { wait?: boolean | undefined; } interface Parseable { parse?: boolean | undefined; } interface PersistenceOptions extends Partial<_Omit> { // TODO: Generalize modelOrCollection success?: ((modelOrCollection: any, response: any, options: any) => void) | undefined; error?: ((modelOrCollection: any, response: any, options: any) => void) | undefined; emulateJSON?: boolean | undefined; emulateHTTP?: boolean | undefined; } interface ModelConstructorOptions extends ModelSetOptions, Parseable { collection?: Collection | undefined; } type CombinedModelConstructorOptions = Model> = ModelConstructorOptions & E; interface ModelSetOptions extends Silenceable, Validable {} interface ModelFetchOptions extends PersistenceOptions, ModelSetOptions, Parseable {} interface ModelSaveOptions extends Silenceable, Waitable, Validable, Parseable, PersistenceOptions { patch?: boolean | undefined; } interface ModelDestroyOptions extends Waitable, PersistenceOptions {} interface CollectionFetchOptions extends PersistenceOptions, Parseable, CollectionSetOptions { reset?: boolean | undefined; } type ObjectHash = Record; interface RoutesHash { [routePattern: string]: string | { (...urlParts: string[]): void }; } /** * DOM events (used in the events property of a View) */ interface EventsHash { [selector: string]: string | { (eventObject: JQuery.TriggeredEvent): void }; } /** * JavaScript events (used in the methods of the Events interface) */ interface EventHandler { (...args: any[]): void; } interface EventMap { [event: string]: EventHandler; } const Events: Events; interface Events extends EventsMixin {} /** * Helper shorthands for classes that implement the Events interface. * Define your class like this: * * import { * Events, * Events_On, * Events_Off, * Events_Trigger, * Events_Listen, * Events_Stop, * } from 'backbone'; * * class YourClass implements Events { * on: Events_On; * off: Events_Off; * trigger: Events_Trigger; * bind: Events_On; * unbind: Events_Off; * * once: Events_On; * listenTo: Events_Listen; * listenToOnce: Events_Listen; * stopListening: Events_Stop; * * // ... (other methods) * } * * Object.assign(YourClass.prototype, Events); // can also use _.extend * * If you are just writing a class type declaration that doesn't already * extend some other base class, you can use the EventsMixin instead; * see below. */ interface Events_On { (this: T, eventName: string, callback: EventHandler, context?: any): T; (this: T, eventMap: EventMap, context?: any): T; } interface Events_Off { (this: T, eventName?: string | null, callback?: EventHandler | null, context?: any): T; } interface Events_Trigger { (this: T, eventName: string, ...args: any[]): T; } interface Events_Listen { (this: T, object: any, events: string, callback: EventHandler): T; (this: T, object: any, eventMap: EventMap): T; } interface Events_Stop { (this: T, object?: any, events?: string, callback?: EventHandler): T; } /** * Helper to avoid code repetition in type declarations. * Backbone.Events cannot be extended, hence a separate abstract * class with a different name. Both classes and interfaces can * extend from this helper class to reuse the signatures. * * For class type declarations that already extend another base * class, and for actual class definitions, please see the * Events_* interfaces above. */ abstract class EventsMixin implements Events { on(eventName: string, callback: EventHandler, context?: any): this; on(eventMap: EventMap, context?: any): this; off(eventName?: string | null, callback?: EventHandler | null, context?: any): this; trigger(eventName: string, ...args: any[]): this; bind(eventName: string, callback: EventHandler, context?: any): this; bind(eventMap: EventMap, context?: any): this; unbind(eventName?: string, callback?: EventHandler, context?: any): this; once(events: string, callback: EventHandler, context?: any): this; once(eventMap: EventMap, context?: any): this; listenTo(object: any, events: string, callback: EventHandler): this; listenTo(object: any, eventMap: EventMap): this; listenToOnce(object: any, events: string, callback: EventHandler): this; listenToOnce(object: any, eventMap: EventMap): this; stopListening(object?: any, events?: string, callback?: EventHandler): this; } class ModelBase extends EventsMixin { parse(response: any, options?: any): any; toJSON(options?: any): any; sync(...arg: any[]): JQueryXHR; } /** * E - Extensions to the model constructor options. You can accept additional constructor options * by listing them in the E parameter. */ class Model extends ModelBase implements Events { /** * Do not use, prefer TypeScript's extend functionality. */ static extend(properties: any, classProperties?: any): any; attributes: Partial; changed: Partial; cidPrefix: string; cid: string; collection: Collection; private _changing: boolean; private _previousAttributes: Partial; private _pending: boolean; /** * Default attributes for the model. It can be an object hash or a method returning an object hash. * For assigning an object hash, do it like this: this.defaults = { attribute: value, ... }; * That works only if you set it in the constructor or the initialize method. */ defaults(): Partial; id: string | number; idAttribute: string; validationError: any; /** * Returns the relative URL where the model's resource would be located on the server. */ url: () => string; urlRoot: _Result; /** * For use with models as ES classes. If you define a preinitialize * method, it will be invoked when the Model is first created, before * any instantiation logic is run for the Model. * @see https://backbonejs.org/#Model-preinitialize */ preinitialize(attributes?: T, options?: CombinedModelConstructorOptions): void; constructor(attributes?: T, options?: CombinedModelConstructorOptions); initialize(attributes?: T, options?: CombinedModelConstructorOptions): void; fetch(options?: ModelFetchOptions): JQueryXHR; /** * For strongly-typed access to attributes, use the `get` method only privately in public getter properties. * @example * get name(): string { * return super.get("name"); * } */ get>(attributeName: A): T[A] | undefined; /** * For strongly-typed assignment of attributes, use the `set` method only privately in public setter properties. * @example * set name(value: string) { * super.set("name", value); * } */ set>(attributeName: A, value?: T[A], options?: S): this; set(attributeName: Partial, options?: S): this; set>(attributeName: A | Partial, value?: T[A] | S, options?: S): this; /** * Return an object containing all the attributes that have changed, or * false if there are no changed attributes. Useful for determining what * parts of a view need to be updated and/or what attributes need to be * persisted to the server. Unset attributes will be set to undefined. * You can also pass an attributes object to diff against the model, * determining if there *would be* a change. */ changedAttributes(attributes?: Partial): Partial | false; clear(options?: Silenceable): this; clone(): Model; destroy(options?: ModelDestroyOptions): JQueryXHR | false; escape(attribute: _StringKey): string; has(attribute: _StringKey): boolean; hasChanged(attribute?: _StringKey): boolean; isNew(): boolean; isValid(options?: any): boolean; previous>(attribute: A): T[A] | null | undefined; previousAttributes(): Partial; save(attributes?: Partial | null, options?: ModelSaveOptions): JQueryXHR; unset(attribute: _StringKey, options?: Silenceable): this; validate(attributes: Partial, options?: any): any; private _validate(attributes: Partial, options: any): boolean; // mixins from underscore keys(): string[]; values(): any[]; pairs(): any[]; invert(): any; pick>(keys: A[]): Partial>; pick>(...keys: A[]): Partial>; pick(fn: (value: any, key: any, object: any) => any): Partial; omit>(keys: A[]): Partial<_Omit>; omit>(...keys: A[]): Partial<_Omit>; omit(fn: (value: any, key: any, object: any) => any): Partial; chain(): any; isEmpty(): boolean; matches(attrs: any): boolean; } class Collection extends ModelBase implements Events { /** * Do not use, prefer TypeScript's extend functionality. */ static extend(properties: any, classProperties?: any): any; model: (new(...args: any[]) => TModel) | ((...args: any[]) => TModel); models: TModel[]; length: number; /** * For use with collections as ES classes. If you define a preinitialize * method, it will be invoked when the Collection is first created and * before any instantiation logic is run for the Collection. * @see https://backbonejs.org/#Collection-preinitialize */ preinitialize(models?: TModel[] | Array>, options?: any): void; constructor(models?: TModel[] | Array>, options?: any); initialize(models?: TModel[] | Array>, options?: any): void; fetch(options?: CollectionFetchOptions): JQueryXHR; /** * Specify a model attribute name (string) or function that will be used to sort the collection. */ comparator: | string | { bivarianceHack(element: TModel): number | string }["bivarianceHack"] | { bivarianceHack(compare: TModel, to?: TModel): number }["bivarianceHack"]; add(models: Array<{} | TModel>, options?: AddOptions): TModel[]; add(model: {} | TModel, options?: AddOptions): TModel; at(index: number): TModel; /** * Get a model from a collection, specified by an id, a cid, or by passing in a model. */ get(id: number | string | Model): TModel; has(key: number | string | Model): boolean; clone(): this; create(attributes: any, options?: ModelSaveOptions): TModel; pluck(attribute: string): any[]; push(model: TModel, options?: AddOptions): TModel; pop(options?: Silenceable): TModel; remove(model: {} | TModel, options?: Silenceable): TModel; remove(models: Array<{} | TModel>, options?: Silenceable): TModel[]; reset(models?: Array<{} | TModel>, options?: Silenceable): TModel[]; /** * The set method performs a "smart" update of the collection with the passed list of models. * If a model in the list isn't yet in the collection it will be added; if the model is already in the * collection its attributes will be merged; and if the collection contains any models that aren't present * in the list, they'll be removed. All of the appropriate "add", "remove", and "change" events are fired as * this happens. Returns the touched models in the collection. If you'd like to customize the behavior, you can * disable it with options: {add: false}, {remove: false}, or {merge: false}. * @param models * @param options */ set(models?: Array<{} | TModel>, options?: CollectionSetOptions): TModel[]; shift(options?: Silenceable): TModel; sort(options?: Silenceable): this; unshift(model: TModel, options?: AddOptions): TModel; where(properties: any): TModel[]; findWhere(properties: any): TModel; modelId(attrs: any): any; values(): Iterator; keys(): Iterator; entries(): Iterator<[any, TModel]>; [Symbol.iterator](): Iterator; private _prepareModel(attributes?: any, options?: any): any; private _removeReference(model: TModel): void; private _onModelEvent(event: string, model: TModel, collection: Collection, options: any): void; private _isModel(obj: any): obj is Model; /** * Return a shallow copy of this collection's models, using the same options as native Array#slice. */ slice(min?: number, max?: number): TModel[]; // mixins from underscore all(iterator?: _.ListIterator, context?: any): boolean; any(iterator?: _.ListIterator, context?: any): boolean; chain(): any; collect(iterator: _.ListIterator, context?: any): TResult[]; contains(value: TModel): boolean; countBy(iterator?: _.ListIterator): _.Dictionary; countBy(iterator: string): _.Dictionary; detect(iterator: _.ListIterator, context?: any): TModel; difference(others: TModel[]): TModel[]; drop(n?: number): TModel[]; each(iterator: _.ListIterator, context?: any): TModel[]; every(iterator: _.ListIterator, context?: any): boolean; filter(iterator: _.ListIterator, context?: any): TModel[]; find(iterator: _.ListIterator, context?: any): TModel; findIndex(predicate: _.ListIterator, context?: any): number; findLastIndex(predicate: _.ListIterator, context?: any): number; first(): TModel; first(n: number): TModel[]; foldl(iterator: _.MemoIterator, memo?: TResult, context?: any): TResult; foldr(iterator: _.MemoIterator, memo?: TResult, context?: any): TResult; forEach(iterator: _.ListIterator, context?: any): TModel[]; groupBy(iterator: _.ListIterator | string, context?: any): _.Dictionary; head(): TModel; head(n: number): TModel[]; include(value: TModel): boolean; includes(value: TModel): boolean; indexBy(iterator: _.ListIterator, context?: any): _.Dictionary; indexBy(iterator: string, context?: any): _.Dictionary; indexOf(value: TModel, isSorted?: boolean): number; initial(): TModel; initial(n: number): TModel[]; inject(iterator: _.MemoIterator, memo?: TResult, context?: any): TResult; invoke(methodName: string, ...args: any[]): any; isEmpty(): boolean; last(): TModel; last(n: number): TModel[]; lastIndexOf(value: TModel, from?: number): number; map(iterator: _.ListIterator, context?: any): TResult[]; max(iterator?: _.ListIterator, context?: any): TModel; min(iterator?: _.ListIterator, context?: any): TModel; partition(iterator: _.ListIterator): TModel[][]; reduce(iterator: _.MemoIterator, memo?: TResult, context?: any): TResult; reduceRight(iterator: _.MemoIterator, memo?: TResult, context?: any): TResult; reject(iterator: _.ListIterator, context?: any): TModel[]; rest(n?: number): TModel[]; sample(): TModel; sample(n: number): TModel[]; select(iterator: _.ListIterator, context?: any): TModel[]; shuffle(): TModel[]; size(): number; some(iterator?: _.ListIterator, context?: any): boolean; sortBy(iterator?: _.ListIterator, context?: any): TModel[]; sortBy(iterator: string, context?: any): TModel[]; tail(n?: number): TModel[]; take(): TModel; take(n: number): TModel[]; toArray(): TModel[]; /** * Sets the url property (or function) on a collection to reference its location on the server. */ url: _Result; without(...values: TModel[]): TModel[]; } type RouterCallback = (...args: string[]) => void; class Router extends EventsMixin implements Events { /** * Do not use, prefer TypeScript's extend functionality. */ static extend(properties: any, classProperties?: any): any; /** * Routes hash or a method returning the routes hash that maps URLs with parameters to methods on your Router. * For assigning routes as object hash, do it like this: this.routes = { "route": callback, ... }; * That works only if you set it in the constructor or the initialize method. */ routes: _Result; /** * For use with Router as ES classes. If you define a preinitialize method, * it will be invoked when the Router is first created, before any * instantiation logic is run for the Router. * @see https://backbonejs.org/#Router-preinitialize */ preinitialize(options?: RouterOptions): void; constructor(options?: RouterOptions); initialize(options?: RouterOptions): void; route(route: string | RegExp, name: string, callback?: RouterCallback): this; route(route: string | RegExp, callback: RouterCallback): this; navigate(fragment: string, options?: NavigateOptions | boolean): this; execute(callback: RouterCallback, args: string[], name: string): void; protected _bindRoutes(): void; protected _routeToRegExp(route: string): RegExp; protected _extractParameters(route: RegExp, fragment: string): string[]; } const history: History; class History extends EventsMixin implements Events { handlers: any[]; interval: number; start(options?: HistoryOptions): boolean; getHash(window?: Window): string; getFragment(fragment?: string): string; decodeFragment(fragment: string): string; getSearch(): string; stop(): void; route(route: string | RegExp, callback: (fragment: string) => void): number; checkUrl(e?: any): void; getPath(): string; matchRoot(): boolean; atRoot(): boolean; loadUrl(fragmentOverride?: string): boolean; navigate(fragment: string, options?: any): boolean; static started: boolean; options: any; private _updateHash(location: Location, fragment: string, replace: boolean): void; } interface ViewOptions { model?: TModel | undefined; // TODO: quickfix, this can't be fixed easy. The collection does not need to have the same model as the parent view. collection?: Collection | undefined; // was: Collection; el?: TElement | JQuery | string | undefined; id?: string | undefined; attributes?: Record | undefined; className?: string | undefined; tagName?: string | undefined; events?: _Result | undefined; } type ViewEventListener = (event: JQuery.Event) => void; class View extends EventsMixin implements Events { /** * Do not use, prefer TypeScript's extend functionality. */ static extend(properties: any, classProperties?: any): any; /** * For use with views as ES classes. If you define a preinitialize * method, it will be invoked when the view is first created, before any * instantiation logic is run. * @see https://backbonejs.org/#View-preinitialize */ preinitialize(options?: ViewOptions): void; constructor(options?: ViewOptions); initialize(options?: ViewOptions): void; /** * Events hash or a method returning the events hash that maps events/selectors to methods on your View. * For assigning events as object hash, do it like this: this.events = { "event:selector": callback, ... }; * That works only if you set it in the constructor or the initialize method. */ events: _Result; // A conditional type used here to prevent `TS2532: Object is possibly 'undefined'` model: TModel extends Model ? TModel : undefined; collection: Collection; setElement(element: TElement | JQuery): this; id?: string | undefined; cid: string; className?: string | undefined; tagName: string; el: TElement; $el: JQuery; attributes: Record; $(selector: string): JQuery; render(): this; remove(): this; delegateEvents(events?: _Result): this; delegate(eventName: string, selector: string, listener: ViewEventListener): this; undelegateEvents(): this; undelegate(eventName: string, selector?: string, listener?: ViewEventListener): this; protected _removeElement(): void; protected _setElement(el: TElement | JQuery): void; protected _createElement(tagName: string): void; protected _ensureElement(): void; protected _setAttributes(attributes: Record): void; } // SYNC function sync(method: string, model: Model | Collection, options?: JQueryAjaxSettings): any; function ajax(options?: JQueryAjaxSettings): JQueryXHR; let emulateHTTP: boolean; let emulateJSON: boolean; // Utility function noConflict(): typeof Backbone; let $: JQueryStatic; }