Knockuot JS seems to have only considered how to bind (Ko.applybindings ()), but did not consider how to remove the binding, when the DOM content modified, the need to re-bind time, found that there seems to be powerless.
First, the solution
Here's a way to rebind, using Ko.cleannode (<your DOM node>) and then re-binding with ko.applybindings ().
1. View Model
[HTML]View PlainCopyprint?
- <h3>3, change bindings </h3>
- <div id="DivSample3">
- What's your name? <span id= 'span3 ' data-bind=' text:name '></span> <br/>
- <a href="javascript:void (0)" onclick="updatebingding ()"> I'm asking for an alias! </a>
- </div>
2, View-model
[JavaScript]View PlainCopyprint?
- <script type="Text/javascript" >
- var ViewModel = function () {
- this.name = ko.observable ("Zhang San");
- this.aliasname = ko.observable ("three children");
- };
- //var MyModel = new ViewModel ();
- Ko.applybindings (new ViewModel (), document.getElementById (' divSample3 '));
- var ViewModel2 = function () {
- this.name = ko.observable ("Zhang San");
- this.aliasname = ko.observable ("three children");
- };
- //Change bindings
- function updatebingding () {
- //$ ("#span3"). attr ("Data-bind", "text:aliasname"); Using jquery
- var span3 = document.getElementById (' span3 '); //Do not use jquery
- Span3.setattribute ("Data-bind", "Text:aliasname");
- Ko.cleannode (SPAN3);
- Ko.applybindings ( new ViewModel2 (), SPAN3);
- }
- </script>
Second, the question
1, but it is said that there may be problems, one of the problems is that the DOM-related event binding is not possible to remove.
Here's a way for a foreign buddy to use:
[JavaScript]View PlainCopyprint?
- <script lang="JavaScript" >
- Ko.unapplybindings = function ($node, remove) {
- //Unbind Events
- $node. Find ("*"). each (function () {
- $ (this). Unbind ();
- });
- //Remove KO subscriptions and references
- if (remove) {
- Ko.removenode ($node [0]);
- } Else {
- Ko.cleannode ($node [0]);
- }
- };
- </script>
This method uses the JQuery method to remove the bound event before unbinding, and then clears the cached binding configuration with some commonality.
But this method should only be valid for the event binding of jquery, and if the event is bound by another way, it may not be completely removed.
2, it is recommended to use if or with binding to control, using the form below to operate, the flexibility is certainly not as straightforward as the use of JavaScript easy to operate.
<!--ko If:editortype = = ' checkbox '-->\
...
<!--/ko-->\
Iii. Adding and removing bindings
Increasing the binding dynamically adds a DOM node and then binds the DOM node. Remove binding to remove the original binding of the DOM node and not allow the binding operation to take effect again.
1. Add Bindings
View Model:
[HTML]View PlainCopyprint?
- <H3>1, dynamically add bindings </h3>
- <div id="DivSample1">
- <a href="javascript:void (0)" onclick="add_binding ()"> Add binding </a >
- </div>
VM Model:
[HTML]View PlainCopyprint?
- <script type="Text/javascript">
- var viewModel = function () {
- };
- var mymodel = new ViewModel ();
- Adding bindings
- function add_binding () {
- ViewModel Adding properties
- mymodel.des = ko.observable ("This is a demo");
- Adding binding elements
- var html = "<span id= ' add_banding ' data-bind= ' html:des ' ></span>";
- Document.body.innerHTML = Document.body.innerHTML + html;
- var add = document.getElementById ("add_banding");
- Ko.applybindings (MyModel, add);
- }
- </Script>
2. Remove Bindings
View Model:
[HTML]View PlainCopyprint?
- <h3>2, remove bindings </h3>
- <div id="DivSample2">
- original value: <span id= ' span1 ' data-bind= ' text: des ' ></span><br/>&NBSP;&NBSP;
- Control Value:<span id= 'span2 ' data-bind=' text:des '></span>< br/>
- <a href="javascript:void (0)" onclick="Changemodelvalue ()"> Change Model property value </a>
- <a href="javascript:void (0)" onclick="removebingding ()"> Remove the binding of "control value" </ a>
- </div>
VM Model:
[HTML]View PlainCopyprint?
- <script type="Text/javascript">
- var viewModel = function () {
- this.des = ko.observable ("This is a demo");
- };
- var mymodel = new ViewModel ();
- Ko.applybindings (MyModel, document.getElementById ("DivSample2"));
- Change des value
- function Changemodelvalue () {
- Mymodel.des (New Date (). ValueOf ());
- }
- Remove bindings
- function removebingding () {
- var span2 = document.getElementById ("span2");
- Alert (Span2.getattribute (' data-bind '));
- Span2.setattribute ("Data-bind", "" ");
- Alert (Span2.getattribute (' data-bind '));
- Ko.cleannode (SPAN2);
- Ko.applybindings (MyModel, span2);
- }
- </Script>
Description: This example refers to an example of a brother on the web, whose thinking is clear, but the example provided does not really solve the problem of multiple bindings, and thank the brother.
Reference:
1 Clear/remove observable bindings in knockout.js?
2, Knockout dynamic Add, remove bindings
Knockout JS Add, remove, modify bindings