From fe360db51eb69b463a2a8ebffcb27a4dc10c26c0 Mon Sep 17 00:00:00 2001 From: Kyle Scully Date: Tue, 14 May 2019 14:34:32 -0700 Subject: [PATCH] feat(backbone): add preinitialize class method (#35419) --- types/backbone/backbone-tests.ts | 8 ++++++++ types/backbone/index.d.ts | 35 +++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/types/backbone/backbone-tests.ts b/types/backbone/backbone-tests.ts index 937d4f261a..20ca8b2cbe 100644 --- a/types/backbone/backbone-tests.ts +++ b/types/backbone/backbone-tests.ts @@ -69,6 +69,14 @@ class SettingDefaults extends Backbone.Model { } } + // will be invoked when the view is first created, before any instantiation logic is run + preinitialize() { + this.defaults = { + name: "Joe" + } as any; + + } + constructor(attributes?: any, options?: any) { super(attributes, options); // error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. this.defaults = { diff --git a/types/backbone/index.d.ts b/types/backbone/index.d.ts index fc96e7f4a1..180708854c 100644 --- a/types/backbone/index.d.ts +++ b/types/backbone/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for Backbone 1.3.3 +// Type definitions for Backbone 1.4.0 // Project: http://backbonejs.org/ // https://github.com/jashkenas/backbone // Definitions by: Boris Yankov @@ -6,6 +6,7 @@ // kenjiru // jjoekoullas // Julian Gonggrijp +// Kyle Scully // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 @@ -235,6 +236,14 @@ declare namespace Backbone { urlRoot: any; + /** + * 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?: any, options?: any): void; + constructor(attributes?: any, options?: any); initialize(attributes?: any, options?: any): void; @@ -311,6 +320,14 @@ declare namespace Backbone { 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[] | Object[], options?: any): void; + constructor(models?: TModel[] | Object[], options?: any); initialize(models?: TModel[] | Object[], options?: any): void; @@ -452,6 +469,14 @@ declare namespace Backbone { **/ routes: RoutesHash | any; + /** + * 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?: Function): Router; @@ -511,6 +536,14 @@ declare namespace Backbone { **/ public 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;