javascript - How to pass a constant outside of a viewmodel to a Knockout binding? -


given there enum , viewmodel:

var myenum = {    val1 = 1,    val2 = 2 };  var someviewmodel = {}; ko.applybindings(someviewmodel); 

and view:

<div data-bind="template: 'my-template', data: myenum.val1"></div> 

i can't find way pass value of myenum.val1 binding, because knockout looks inside viewmodel. prepending $root.myenum.val1 doesn't work either, still looks within viewmodel.

any ideas how working?

i prefer declare viewmodel function first instead of making them object directly. doing can reuse in future if needed , have neater process pass object viewmodel during initialization.

here example:

function someviewmodel(enum) {   this.myenum = enum; };  var myenum = {   val1: 1,   val2: 2 };  ko.applybindings(new someviewmodel(myenum)); 

then in html can following

<div data-bind="template: 'my-template', data: myenum.val1"></div> 

Comments

Popular posts from this blog

'hasOwnProperty' in javascript -

python - ValueError: No axis named 1 for object type <class 'pandas.core.series.Series'> -

java - How to provide dependency injections in Eclipse RCP 3.x? -