Capqueen
Capqueen

Reputation: 71

knockoutjs re-bind when document reload by ajax

I use a mobile UI Framework which load page by ajax. when change page it remove page dom by $.remove();

and I use knockoutjs to bind data in every page.

the problem is when page A is removed. ko not remove. then I change to A again(From page B). It can not applybinds agin.

Also I try ko.cleanNode(),then rebind but it do not clear foreach nodes,so how can I rebind ko, when document reload by ajax?

Upvotes: 0

Views: 58

Answers (1)

Roy J
Roy J

Reputation: 43899

Stop using jQuery to manipulate the DOM. That is Knockout's only job. Knockout is not a tool to bind data, Knockout is a tool to manage the DOM. Knockout provides tools to do things like swapping out pages (see Swappable Templates, for example)

If you are using cleanNode, you are doing something wrong. If you need to change out your viewmodel entirely, the viewmodel itself can be an observable.

vm = ko.observable(someInitialVm);

ko.applyBindings(vm);

//...at some point...
vm(aWholeDifferentVm);

You can do that. Focus on your viewmodel. Leave the DOM to Knockout.

Upvotes: 1

Related Questions