1

I've faced problem with binding modified at runtime viewmodel through prototype to particular html elements. I've added an array to model, but after applying bindings - nothing happen. Added observables working fine, but problem exactly with arrays.

Here is a jsfiddle with code: click

var ViewModel = function() {
    var self = this;
    this.selectedItems = [{FullName: 'Mike'}]                                   
};
var model = new ViewModel();
ko.applyBindings(model,document.getElementById('node'));
ko.cleanNode(document.getElementById('node'));
model.__proto__.items = [{FullName: 'Michael'}];
ko.applyBindings(model, document.getElementById('node'));

Thx for advice.

vchyzhevskyi
  • 753
  • 1
  • 11
  • 20
  • Regarding your fiddle... Did you mean $data.selectedItems instead of $.data.items? – veritasetratio Feb 08 '14 at 13:19
  • $data.items - an array that added through __proto__ – vchyzhevskyi Feb 08 '14 at 13:22
  • Apologies, but your question (what are you asking exactly? the code in your question does not really [repro](http://sscce.org) an issue) nor fiddle (second @veritasetratio's comment, and even then it does what I'd expect it to do) makes much sense. Could you please edit and clarify? – Jeroen Feb 08 '14 at 14:10

1 Answers1

0

The problem is that ko.cleanNode is not working as you think it is. It is not removing all of the bindings as you think it is.

I would recommend completely removing your node div, and then cloning and re-adding it. Naturally jQuery makes this easy, if you happen to be using that. Otherwise I would back up a bit and really evaluate whether you need to add things to a viewModel's prototype and then re-bind to your div. Are you sure an if binding won't do what you want?

Also, consider testing this code with knockout 3 - I'm confident you'll get an error about applying bindings multiple times.

Community
  • 1
  • 1
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
  • I cannot completely remove node div, because in my project I'm binding value to all page including page title, and some other places in head, so I cannot remove my HTML node ... – vchyzhevskyi Feb 12 '14 at 12:27
  • @coirius - then look at using an if binding to re-generate nodes. Also note that you *can* remove a given node and then re-add it, and re-call applybindings to IT. Or you can call applyBindingsToNode on it, and provide a root context. – Adam Rackis Feb 12 '14 at 19:14