mirror of
https://github.com/wassname/DefinitelyTyped.git
synced 2026-09-09 11:13:57 +08:00
Added definitions for Knockout-ES5 plugin
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
/// <reference path="knockout.es5.d.ts" />
|
||||
|
||||
var empty = {},
|
||||
obj = { a: 'string', b: 123, c: true, d: empty },
|
||||
observable = ko.observable(123),
|
||||
computed = ko.computed(function () { return observable() + 1; }),
|
||||
model = { prop: 100 },
|
||||
notifiedValues = [];
|
||||
|
||||
|
||||
// Basic properties
|
||||
ko.track(empty);
|
||||
ko.track(null);
|
||||
ko.track(obj);
|
||||
ko.track({ a: 1 });
|
||||
ko.track({ prop: observable });
|
||||
ko.track({ prop: computed });
|
||||
ko.track({ a: 1, b: 2, c: 3 }, null);
|
||||
ko.track({ a: 1, b: 2, c: 3 }, []);
|
||||
ko.track({ a: 1, b: 2, c: 3 }, ['a', 'c']);
|
||||
ko.track({ prop: observable }, ['prop']);
|
||||
ko.track({ prop: computed }, ['prop']);
|
||||
ko.track(null, null);
|
||||
ko.track(model);
|
||||
|
||||
// Computed properties
|
||||
ko.defineProperty(model, 'propPlusOne', () => { return model.prop + 1; });
|
||||
ko.defineProperty(model, 'propOne', { get: function () { return 1; } });
|
||||
ko.defineProperty(model, 'propPlusOne', { get: () => model.prop++, set: (value) => model.prop = value - 1 });
|
||||
|
||||
// Utility functions
|
||||
ko.getObservable(model, 'propPlusOne').subscribe((newValue) => notifiedValues.push(newValue));
|
||||
|
||||
|
||||
// Simple usage example
|
||||
class OrderLine {
|
||||
subtotal: string;
|
||||
|
||||
constructor(public item: any, public price: number, public quantity: number) {
|
||||
// Instead of declaring ko.observable properties, we just have one call to ko.track
|
||||
ko.track(this);
|
||||
|
||||
// Computed property
|
||||
ko.defineProperty(model, 'subtotal', () => {
|
||||
return "$" + (this.price * this.quantity).toFixed(2);
|
||||
});
|
||||
|
||||
// Accessing the observables
|
||||
ko.getObservable(this, "price").subscribe(function (newPrice) {
|
||||
console.log('The new price is ' + newPrice);
|
||||
});
|
||||
}
|
||||
|
||||
public getSubtotal(): string {
|
||||
return "$" + (this.price * this.quantity).toFixed(2);
|
||||
}
|
||||
}
|
||||
|
||||
class Order {
|
||||
lines: Array<OrderLine> = [];
|
||||
|
||||
constructor() {
|
||||
ko.track(this, ["lines"]);
|
||||
}
|
||||
}
|
||||
|
||||
var someOrderLine = new OrderLine(null, 0.445, 11), anOrder = new Order();
|
||||
|
||||
someOrderLine.item = "Test Item";
|
||||
someOrderLine.quantity += 1;
|
||||
|
||||
anOrder.lines.push(someOrderLine);
|
||||
anOrder.lines.push(someOrderLine);
|
||||
anOrder.lines.shift();
|
||||
|
||||
console.log(someOrderLine.subtotal == someOrderLine.getSubtotal()); // true
|
||||
console.log(anOrder.lines.length); // 1
|
||||
Vendored
+14
@@ -0,0 +1,14 @@
|
||||
// Type definitions for Knockout-ES5
|
||||
// Project: https://github.com/SteveSanderson/knockout-es5
|
||||
// Definitions by: Sebastián Galiano <https://github.com/sgaliano/>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
/// <reference path="../knockout/knockout.d.ts" />
|
||||
|
||||
interface KnockoutStatic {
|
||||
track(obj: any, propertyNames?: Array<string>): any;
|
||||
defineProperty(obj: any, propertyName: string, evaluator: Function): any;
|
||||
defineProperty(obj: any, propertyName: string, options: { get: () => any; set?: (value: any) => void; }): any;
|
||||
getObservable(obj: any, propertyName: string): KnockoutObservable;
|
||||
valueHasMutated(obj: any, propertyName: string): void;
|
||||
}
|
||||
Reference in New Issue
Block a user