From 74ba1baa4bd0f1415015f0c17a04aed76675b311 Mon Sep 17 00:00:00 2001 From: HowardRichards Date: Fri, 13 Feb 2015 12:10:20 +0000 Subject: [PATCH 1/2] Created new definitions for ko.plus library ko.plus is a set of extensions for knockout.js --- ko.plus/ko.plus-tests.ts | 128 ++++++++++++++++++++++++++++++ ko.plus/ko.plus.d.ts | 167 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 295 insertions(+) create mode 100644 ko.plus/ko.plus-tests.ts create mode 100644 ko.plus/ko.plus.d.ts diff --git a/ko.plus/ko.plus-tests.ts b/ko.plus/ko.plus-tests.ts new file mode 100644 index 000000000..952b286d2 --- /dev/null +++ b/ko.plus/ko.plus-tests.ts @@ -0,0 +1,128 @@ +/// +/// + +// Tests for ko.plus.d.ts +// Project: https://github.com/stevegreatrex/ko.plus +// Definitions by: Howard Richards +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/* + Version 1.0 - initial commit + + Note: Typescript version 1.4 or higher is required for union types + and type declarations +*/ + +function CommandTests() { + + // initalize command with an execute method + var cmd1 = ko.command(() => { + return "Hello cmd1"; + }); + + // initialize command and add done and fail callbacks + var cmd2 = ko.command(() => { + return "Hello cmd2"; + }) + .done((data: any) => { + alert("success"); + }) + .fail((error: string) => { + alert(error); + }); + + // initialize command with options (action only) + var cmd3 = ko.command({ + action: () => { return "Hello cmd3"; } + }); + + // initialize command with options (action only) + var cmd4 = ko.command({ + action: () => { return "Hello cmd4"; } + }); + + + // test execute the command + cmd1(); + + // test properties of the commmand + var isRunning = cmd1.isRunning(); + var failed = cmd1.failed(); + var completed = cmd1.completed(); + var canExecute = cmd1.canExecute(); + +} + +function EditableTests() { + + // test ko.editable initializers + var edit1 = ko.editable(); // no intializer + + var edit2 = ko.editable("test"); // with typed initializer + + var edit3 = ko.editable({ test: true }); // with anything + + var edit4 = ko.editable(1); // with union types + var edit5 = ko.editable("test"); + + // test getting the value + var value = edit1(); + + // test editable + var isEditing = edit1.isEditing(); + + // test editableArray functions: + edit1.beginEdit(); + edit1.endEdit(); + edit1.cancelEdit(); + edit1.rollback(); +} + +function EditableArrayTests() { + + // test ko.editable intializers + var edit1 = ko.editableArray(); // no init value + + var edit2 = ko.editableArray([1, 2, 3]); // init value + + var edit3 = ko.editableArray(["a", 1, false, {}]); // mixed + + var edit4 = ko.editableArray(["a", 1]); // constrained + + // test getting the array value + var value = edit1(); + + // test properties + var isEditing = edit1.isEditing(); + + // test functions: + edit1.beginEdit(); + edit1.endEdit(); + edit1.cancelEdit(); + edit1.rollback(); + +} + +function SortableTests() { + + // sorting is added via an extender, there are no .d.ts + // types for this at present + var sort1 = ko.observableArray([1, 2, 3]).extend({ sortable: true }); + + // extended sort definition with key+descending + var sort2 = ko.observableArray([ + { id: 3, name: "alice" }, + { id: 2, name: "james" }, + { id: 1, name: "bob" }, + ]).extend({ + sortable: { + key: 'id', + descending: false + } + }); + + sort2.setSourceKey("id"); + sort2.sortDescending(true); + sort2.setSourceKey("name"); + sort2.sortDescending(false); +} \ No newline at end of file diff --git a/ko.plus/ko.plus.d.ts b/ko.plus/ko.plus.d.ts new file mode 100644 index 000000000..734558d01 --- /dev/null +++ b/ko.plus/ko.plus.d.ts @@ -0,0 +1,167 @@ +// Type definitions for ko.plus v0.0.21 +// Project: https://github.com/stevegreatrex/ko.plus +// Definitions by: Howard Richards +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +/** + * + * Extensions to KO to provide a command, editable and sortable patterns + * - available at http://www.nuget.org/packages/ko.plus/ + * + * (requires TypeScript version 1.4 or higher) + * + * Version 1.0 - initial commit + * + */ + + +// +// Add methods to the 'ko' Knockout object +// +interface KnockoutStatic { + + // create a command - two overloads + command: (param: KoPlus.Callback | KoPlus.CommandOptions) => KoPlus.Command; + + editable: KoPlus.EditableStatic; + editableArray: KoPlus.EditableArrayStatic; +} + + +//#region Sortable type extensions + +// +// extends the KnockoutObservableArray to add sorting methods +// see https://github.com/stevegreatrex/ko.plus#properties-and-functions +// +interface KnockoutObservableArray { + sortKey: KnockoutObservable; + + sortDescending: KnockoutObservable; + + setSourceKey: (key: string) => void; +} + +//#endregion + + +// +// declare new binding handlers in ko-plus +// +interface KnockoutBindingHandlers { + + loadingWhen: KnockoutBindingHandler; + + command: KnockoutBindingHandler; + + sortBy: KnockoutBindingHandler; +} + +// +// namespace for ko-plus types +// +declare module KoPlus { + + // predefine a callback type + export type Callback = () => void; + + + //#region Command types + + // + // the Command interface - returned by ko.command(..) + // + export interface Command { + // execute the command + (): void; + + // + // properties: https://github.com/stevegreatrex/ko.plus#properties + // + isRunning: KnockoutObservable; + + canExecute: KnockoutComputed; + + failed: KnockoutObservable; + + completed: KnockoutObservable; + + // + // functions + // see https://github.com/stevegreatrex/ko.plus#functions + // + done: (callback: (data: any) => void) => Command; + + fail: (callback: (error: string) => void) => Command; + + always: (callback: Callback) => Command; + + then: (resolve: Callback, reject: Callback) => Command; + + } + + // + // options when defining a command using the option method + // see https://github.com/stevegreatrex/ko.plus#options + // + export interface CommandOptions { + + // [required] sets the command action method + action: Callback; + + // [optional] function to determine if command can be executed + canExecute?: () => boolean; + + // [optional] context to use in the command + context?: any; + } + + //#endregion + + //#region Editable types + + export interface EditableArrayStatic { + fn: KnockoutObservableArrayFunctions; + (value?: Array): EditableArray; + } + + export interface EditableStatic { + fn: KnockoutObservableFunctions; + (value?: T): Editable; + + makeEditable(target: any): void; + } + + // + // defines common editable functions and isEditing property + // + export interface EditableBase extends KnockoutObservableFunctions { + isEditing: KnockoutObservable; + beginEdit(): void; + endEdit(): void; + cancelEdit(): void; + rollback(): void; + } + + export interface Editable extends EditableBase { + (): T; + (value: T): void; + + subscribe(callback: (newValue: T) => void, target?: any, topic?: string): KnockoutSubscription; + notifySubscribers(valueToWrite: T, topic?: string): void; + } + + export interface EditableArray extends KnockoutObservableArrayFunctions, EditableBase { + (): T[]; + (value: T[]): void; + + subscribe(callback: (newValue: T[]) => void, target?: any, topic?: string): KnockoutSubscription; + notifySubscribers(valueToWrite: T[], topic?: string): void; + } + + + //#endregion + +} \ No newline at end of file From 2a67c1e9235e065efb69d4a661844734bf32b0ab Mon Sep 17 00:00:00 2001 From: HowardRichards Date: Wed, 18 Feb 2015 11:19:52 +0000 Subject: [PATCH 2/2] Fixed bug - makeEditable now on .editable Also refactored how editable types inherit - now simpler --- ko.plus/ko.plus-tests.ts | 4 +++ ko.plus/ko.plus.d.ts | 58 ++++++++++++++++------------------------ 2 files changed, 27 insertions(+), 35 deletions(-) diff --git a/ko.plus/ko.plus-tests.ts b/ko.plus/ko.plus-tests.ts index 952b286d2..992332e2b 100644 --- a/ko.plus/ko.plus-tests.ts +++ b/ko.plus/ko.plus-tests.ts @@ -9,6 +9,8 @@ /* Version 1.0 - initial commit + Version 1.1 - added test for makeEditable + Note: Typescript version 1.4 or higher is required for union types and type declarations */ @@ -65,6 +67,8 @@ function EditableTests() { var edit4 = ko.editable(1); // with union types var edit5 = ko.editable("test"); + ko.editable.makeEditable(this); + // test getting the value var value = edit1(); diff --git a/ko.plus/ko.plus.d.ts b/ko.plus/ko.plus.d.ts index 734558d01..65301aa7c 100644 --- a/ko.plus/ko.plus.d.ts +++ b/ko.plus/ko.plus.d.ts @@ -14,14 +14,14 @@ * * Version 1.0 - initial commit * + * Version 1.1 - fixed bug - makeEditable is now a function on .editable + * also refactored how the Editable classes inherit to simplify */ - // // Add methods to the 'ko' Knockout object // interface KnockoutStatic { - // create a command - two overloads command: (param: KoPlus.Callback | KoPlus.CommandOptions) => KoPlus.Command; @@ -29,7 +29,6 @@ interface KnockoutStatic { editableArray: KoPlus.EditableArrayStatic; } - //#region Sortable type extensions // @@ -46,12 +45,10 @@ interface KnockoutObservableArray { //#endregion - // -// declare new binding handlers in ko-plus +// declare new binding handlers in ko.plus // interface KnockoutBindingHandlers { - loadingWhen: KnockoutBindingHandler; command: KnockoutBindingHandler; @@ -60,14 +57,12 @@ interface KnockoutBindingHandlers { } // -// namespace for ko-plus types +// namespace for ko.plus types // declare module KoPlus { - // predefine a callback type export type Callback = () => void; - //#region Command types // @@ -78,7 +73,7 @@ declare module KoPlus { (): void; // - // properties: https://github.com/stevegreatrex/ko.plus#properties + // properties: https://github.com/stevegreatrex/ko.plus#properties // isRunning: KnockoutObservable; @@ -89,7 +84,7 @@ declare module KoPlus { completed: KnockoutObservable; // - // functions + // functions // see https://github.com/stevegreatrex/ko.plus#functions // done: (callback: (data: any) => void) => Command; @@ -99,7 +94,6 @@ declare module KoPlus { always: (callback: Callback) => Command; then: (resolve: Callback, reject: Callback) => Command; - } // @@ -107,7 +101,6 @@ declare module KoPlus { // see https://github.com/stevegreatrex/ko.plus#options // export interface CommandOptions { - // [required] sets the command action method action: Callback; @@ -122,22 +115,23 @@ declare module KoPlus { //#region Editable types - export interface EditableArrayStatic { - fn: KnockoutObservableArrayFunctions; - (value?: Array): EditableArray; - } - - export interface EditableStatic { - fn: KnockoutObservableFunctions; + export interface EditableStatic extends KnockoutObservableStatic { (value?: T): Editable; makeEditable(target: any): void; } + export interface EditableArrayStatic extends KnockoutObservableArrayStatic { + (value?: Array): EditableArray; + + makeEditable(target: any): void; //> + } + // // defines common editable functions and isEditing property + // (used by both editable and editableArray // - export interface EditableBase extends KnockoutObservableFunctions { + export interface EditableFunctions { isEditing: KnockoutObservable; beginEdit(): void; endEdit(): void; @@ -145,23 +139,17 @@ declare module KoPlus { rollback(): void; } - export interface Editable extends EditableBase { - (): T; - (value: T): void; - - subscribe(callback: (newValue: T) => void, target?: any, topic?: string): KnockoutSubscription; - notifySubscribers(valueToWrite: T, topic?: string): void; + // + // extend the standard KnockoutObservable to add editable functions + // + export interface Editable extends KnockoutObservable, EditableFunctions { } - export interface EditableArray extends KnockoutObservableArrayFunctions, EditableBase { - (): T[]; - (value: T[]): void; - - subscribe(callback: (newValue: T[]) => void, target?: any, topic?: string): KnockoutSubscription; - notifySubscribers(valueToWrite: T[], topic?: string): void; + // + // extend the standard KnockoutObservableArray to add editable functions + // + export interface EditableArray extends KnockoutObservableArray, EditableFunctions { } - //#endregion - } \ No newline at end of file