0

I Have viewModel

var viewModel = {
    amount: ko.observable(1),
    rate: ko.observable(2),
    rate222: ko.observable(2)
};


<input data-bind="value: amount" />
<input data-bind="value: rate" />

How do I know that rate222 not bind on this document?

  • For need it for multi-page validation, for declared on exist document!

best regards


UPD:

This does not resolve this problem here jsfiddle.net/x26sS/14 Bind value "rate222" not in the DOM, but the knockout does not think so: "show,rate12312".

zoh
  • 175
  • 3
  • 13

1 Answers1

0

There is not a great way in Knockout to know specifically that an element is bound out-of-the-box.

It would be pretty easy to write a custom binding that adds a bound flag to an observable or computed that you could then use when looking for "unbound" properties.

The binding could just look like:

ko.bindingHandlers.track = {
    init: function(element, valueAccessor) {
       var observable = valueAccessor();
       if (ko.isObservable(observable)) {
           observable.bound = true;  
       }

       //clear the flag if this element is removed
       ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
            var observable = valueAccessor();
            if (ko.isObservable(observable)) {
                observable.bound = false;     
            }     
       });           
    }
};

You could even wrap the value binding to add this functionality into it.

Here is a sample that uses this binding on the elements and then provides a function to loop through view model (only top-level in sample) and find any unbound properties.

http://jsfiddle.net/rniemeyer/x26sS/

RP Niemeyer
  • 114,592
  • 18
  • 291
  • 211
  • This does not resolve this problem here http://jsfiddle.net/x26sS/14/ Bind value "rate222" not in the DOM, but the knockout does not think so: "show,rate12312". – zoh Oct 11 '12 at 07:27
  • The issue that you are seeing is because when you trigger a notification to `data`'s subscribers, this actually re-renders your template which is happening after your manual subscription, so the bound boolean is set again. A good way to handle this would be to add a disposal callback to your binding that clears it when the element is removed like: http://jsfiddle.net/rniemeyer/x26sS/17/ – RP Niemeyer Oct 11 '12 at 13:07