knockout.js - Declaring a knockout computed observable in typescript -
i new typescript , combine goodness of knockout. have computed observable, works, want know right way or there better way.
i using knockout definition file nu-get. in there 4 knockoutcomputed(x) definitions.
- knockoutcomputed
- knockoutcomputeddefine
- knockoutcomputedfunctions
- knockoutcomputedstatic
i {} method of declaring observable's , keep this. long story short correct method of declaring observables or there alternate way (maybe intlisense in function)
i using first so:
class personviewmodel { public firstname: knockoutobservable<string>; public lastname: knockoutobservable<string>; public fullname: knockoutcomputed<string>; constructor() { this.firstname = ko.observable(''); this.lastname = ko.observable(''); this.fullname = ko.computed({ owner: this, read: function () { return this.firstname() + " " + this.lastname(); } }); } } with html snippet of:
<h2>type script , knockout.</h2> <input data-bind="value: firstname" /> <input data-bind="value: lastname" /> <div data-bind="text: fullname"></div>
recommendation use arrow functions computed. give desired intellisence well:
this.fullname = ko.computed({ owner: this, read: () => { return this.firstname() + " " + this.lastname(); } }); basically captures this using closure doesn't matter calls function. this continue mean personviewmodel instead of any . more : http://basarat.github.io/typescriptdeepdive/#/this
try intellisense in typescript playground. should intellisense when press this.
Comments
Post a Comment