Knockout JS Add, remove, modify bindings

Source: Internet
Author: User

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?
  1. <h3>3, change bindings </h3>
  2. <div id="DivSample3">
  3. What's your name? <span id= 'span3 ' data-bind=' text:name '></span> <br/>
  4. <a href="javascript:void (0)" onclick="updatebingding ()"> I'm asking for an alias! </a>
  5. </div>

2, View-model

[JavaScript]View PlainCopyprint?
  1. <script type="Text/javascript" >
  2. var ViewModel = function () {
  3. this.name = ko.observable ("Zhang San");
  4. this.aliasname = ko.observable ("three children");
  5. };
  6. //var MyModel = new ViewModel ();
  7. Ko.applybindings (new ViewModel (), document.getElementById (' divSample3 '));
  8. var ViewModel2 = function () {
  9. this.name = ko.observable ("Zhang San");
  10. this.aliasname = ko.observable ("three children");
  11. };
  12. //Change bindings
  13. function updatebingding () {
  14. //$ ("#span3"). attr ("Data-bind", "text:aliasname"); Using jquery
  15. var span3 = document.getElementById (' span3 '); //Do not use jquery
  16. Span3.setattribute ("Data-bind", "Text:aliasname");
  17. Ko.cleannode (SPAN3);
  18. Ko.applybindings ( new ViewModel2 (), SPAN3);
  19. }
  20. </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?
  1. <script lang="JavaScript" >
  2. Ko.unapplybindings = function ($node, remove) {
  3. //Unbind Events
  4. $node. Find ("*"). each (function () {
  5. $ (this). Unbind ();
  6. });
  7. //Remove KO subscriptions and references
  8. if (remove) {
  9. Ko.removenode ($node [0]);
  10. } Else {
  11. Ko.cleannode ($node [0]);
  12. }
  13. };
  14. </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?
  1. <H3>1, dynamically add bindings </h3>
  2. <div id="DivSample1">
  3. <a href="javascript:void (0)" onclick="add_binding ()"> Add binding </a >
  4. </div>


VM Model:

[HTML]View PlainCopyprint?
  1. <script type="Text/javascript">
  2. var viewModel = function () {
  3. };
  4. var mymodel = new ViewModel ();
  5. Adding bindings
  6. function add_binding () {
  7. ViewModel Adding properties
  8. mymodel.des = ko.observable ("This is a demo");
  9. Adding binding elements
  10. var html = "<span id= ' add_banding ' data-bind= ' html:des ' ></span>";
  11. Document.body.innerHTML = Document.body.innerHTML + html;
  12. var add = document.getElementById ("add_banding");
  13. Ko.applybindings (MyModel, add);
  14. }
  15. </Script>


2. Remove Bindings

View Model:

[HTML]View PlainCopyprint?
  1. <h3>2, remove bindings </h3>
  2. <div id="DivSample2">
  3.      original value: <span id= ' span1 '  data-bind= ' text: des ' ></span><br/>&NBSP;&NBSP;
  4. Control Value:<span id= 'span2 ' data-bind=' text:des '></span>< br/>
  5. <a href="javascript:void (0)" onclick="Changemodelvalue ()"> Change Model property value </a>
  6. <a href="javascript:void (0)" onclick="removebingding ()"> Remove the binding of "control value" </ a>
  7. </div>


VM Model:

[HTML]View PlainCopyprint?
  1. <script type="Text/javascript">
  2. var viewModel = function () {
  3. this.des = ko.observable ("This is a demo");
  4. };
  5. var mymodel = new ViewModel ();
  6. Ko.applybindings (MyModel, document.getElementById ("DivSample2"));
  7. Change des value
  8. function Changemodelvalue () {
  9. Mymodel.des (New Date (). ValueOf ());
  10. }
  11. Remove bindings
  12. function removebingding () {
  13. var span2 = document.getElementById ("span2");
  14. Alert (Span2.getattribute (' data-bind '));
  15. Span2.setattribute ("Data-bind", "" ");
  16. Alert (Span2.getattribute (' data-bind '));
  17. Ko.cleannode (SPAN2);
  18. Ko.applybindings (MyModel, span2);
  19. }
  20. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.