Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding observable properties to model after ko.applyBindings in knockout.js

For example

function Model(){};
var model = new Model();
ko.applyBinding(model);

model.name = ko.observable('john');

I know the code will not work but is it possible to make it work ?

like image 907
Freshblood Avatar asked Nov 14 '22 06:11

Freshblood


1 Answers

I was able to accomplish this by using cleanNode and then reapplying the bindings.

function Model(){};
var model = new Model();
ko.applyBindings(model);

model.name = ko.observable('john');

var myDiv = $('#myDiv')[0];
ko.cleanNode(myDiv);
ko.applyBindings(model, myDiv);

<div id="myDiv">
    <span data-bind="text: name"></span>
</div>
like image 186
schettino72 Avatar answered Nov 16 '22 04:11

schettino72