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.

  1. knockoutcomputed
  2. knockoutcomputeddefine
  3. knockoutcomputedfunctions
  4. 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

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -