diff --git a/knockout/knockout.d.ts b/knockout/knockout.d.ts index 48f550dfe..494b6f844 100644 --- a/knockout/knockout.d.ts +++ b/knockout/knockout.d.ts @@ -556,7 +556,13 @@ interface KnockoutBindingProvider { } interface KnockoutComponents { - register(componentName: string, definition: KnockoutComponentDefinition): void; + // overloads for register method: + register(componentName: string, config: KnockoutComponentRegister): void; + register(componentName: string, config: KnockoutComponentRegisterStringTemplate): void; + register(componentName: string, config: KnockoutComponentRegisterFnViewModel): void; + register(componentName: string, config: KnockoutComponentRegisterStringTemplateFnViewModel): void; + register(componentName: string, config: KnockoutComponentRegisterAMD): void; + isRegistered(componentName: string): boolean; unregister(componentName: string): void; get(componentName: string, callback: (definition: KnockoutComponentDefinition) => void): void; @@ -566,6 +572,50 @@ interface KnockoutComponents { getComponentNameForNode(node: Node): string; } +/* interfaces for register overloads*/ + +interface KnockoutComponentRegister { + template: KnockoutComponentTemplate; + viewModel?: KnockoutComponentConfigViewModel; +} + +interface KnockoutComponentRegisterAMD { + // load self-describing module using AMD module name + require: string; +} + +interface KnockoutComponentRegisterFnViewModel { + template: KnockoutComponentTemplate; + viewModel?: (params: any) => any; +} + +interface KnockoutComponentRegisterStringTemplate { + template: string; + viewModel?: KnockoutComponentConfigViewModel; +} + +interface KnockoutComponentRegisterStringTemplateFnViewModel { + template: string; + viewModel?: (params: any) => any; +} + +interface KnockoutComponentConfigViewModel { + instance?: any; + createViewModel? (params?: any, componentInfo?: KnockoutComponentInfo): any; + require?: string; +} + +interface KnockoutComponentTemplate { + // specify element id (string) or a node + element?: any; + // AMD module load + require?: string; +} + +interface KnockoutComponentInfo { + element: any; +} +/* end register overloads */ interface KnockoutComponentDefinition { template: Node[]; createViewModel?(params: any, options: { element: Node; }): any; diff --git a/knockout/tests/knockout-tests.ts b/knockout/tests/knockout-tests.ts index 6d4c9e25d..82b5aa25c 100644 --- a/knockout/tests/knockout-tests.ts +++ b/knockout/tests/knockout-tests.ts @@ -602,4 +602,49 @@ function test_allBindingsAccessor() { var fnAccessorBinding = allBindingsAccessor().myBindingName; } }; +} + +function test_Components() { + + function test_Register() { + // test all possible ko.components.register() overloads + var nodeArray = [new Node, new Node]; + var singleNode = new Node; + + // ------- string-templates with different viewmodel overloads: + + // string template and inline function (commonly used in examples) + ko.components.register("name", { template: "string-template", viewModel: function (params) { return null; } }); + + // string template and instance vm + ko.components.register("name", { template: "string-template", viewModel: { instance: null } }); + + // string template and createViewModel factory method + ko.components.register("name", { template: "string-template", viewModel: { createViewModel: function (params: any, componentInfo: KnockoutComponentInfo) { return null; } } }); + + // string template and require module vm + ko.components.register("name", { template: "string-template", viewModel: { require: "module" } }); + + // ------- non-string templates + + // viewmodel as function and four types of template + ko.components.register("name", { template: { element: "elementID" }, viewModel: function (params) { return null; } }); + // Node template for element and inline function (commonly used in examples) + ko.components.register("name", { template: { element: singleNode }, viewModel: function (params) { return null; } }); + // object template for element and inline function (commonly used in examples) + ko.components.register("name", { template: nodeArray, viewModel: function (params) { return null; } }); + // object template for element and inline function (commonly used in examples) + ko.components.register("name", { template: { require: "module" }, viewModel: function (params) { return null; } }); + + // viewmodel as object, and four types of non-string tempalte + ko.components.register("name", { template: { element: "elementID" }, viewModel: { instance: null } }); + // Node template for element and inline function (commonly used in examples) + ko.components.register("name", { template: { element: singleNode }, viewModel: { instance: null } }); + // object template for element and inline function (commonly used in examples) + ko.components.register("name", { template: nodeArray, viewModel: { instance: null } }); + // object template for element and inline function (commonly used in examples) + ko.components.register("name", { template: { require: "module" }, viewModel: { instance: null } }); + + // + } } \ No newline at end of file