Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

knockout.js - Can cleanNode() be used to clean binding?

With below code, input#p_in will be updated with the change of input#s_in. But I have used cleanNode(sec). Could anyone help understand why the binding is not cleared.

    <input id="p_in" data-bind="value: name"></input>
    <input id="s_in" data-bind="value: name"></input>
    <input id="cb" type="checkbox">same</input>

    <script type="text/javascript">
        function AddrDataSet (name) {
            this.name = ko.observable(name);
        };

        var primary_set = new AddrDataSet('p');
        var sec_set = new AddrDataSet('s');
        var pri = $('#p_in')[0];
        var sec = $('#s_in')[0];

        ko.applyBindings(primary_set, pri);
        ko.applyBindings(sec_set, sec);

        ko.cleanNode(sec); // clean it
        ko.applyBindings(primary_set, sec); // bind it to primary_set
        ko.cleanNode(sec); // clean it again

    </script>
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

ko.cleanNode is used internally by Knockout to clean up data/computeds that it created related to the element. It does not remove any event handlers added by bindings or necessarily understand if a binding made changes to the DOM. This can definitely cause problems like having multiple handlers attached to an element when it is subsequently bound again.

So, I would not recommend using this pattern. A better pattern is to use with or the template binding around a section and allow it to be re-rendered with the new bindings.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...