How to know if ko.applyBindings has been called?

650 views
Skip to first unread message

smartg...@gmail.com

unread,
Apr 16, 2013, 9:40:08 AM4/16/13
to knock...@googlegroups.com
Hello all,
 
I'v noticed that calling twice ko.applyBindings could break the bindings mechanism.
 
So is there a way to know if ko.applyBindigs had been called using  a ko object or even the viewModel ?
 
A quick fix is using a variable that I set after calling the method, but that seems not that clean to me.
 
Any ideas,
 
Thx,
 
charly
 
 

Hasen el Judy

unread,
Apr 16, 2013, 12:32:55 PM4/16/13
to knock...@googlegroups.com
Sugar.js and underscore.js both offer wrappers that guarantee a function only gets executed once:


Example (straight from the sugarjs docs):


var fn = (function() {
    // Will be executed once only
 
}).once(); fn(); fn(); fn();

smartg...@gmail.com

unread,
Apr 17, 2013, 7:13:57 AM4/17/13
to knock...@googlegroups.com
I will check these frameworks out.

Michael Best

unread,
Apr 18, 2013, 5:02:38 PM4/18/13
to knock...@googlegroups.com
ko.dataFor or ko.contextFor, described at http://knockoutjs.com/documentation/unobtrusive-event-handling.html, will tell you whether an element or an ancestor has been "bound". But this will return a false positive for elements that aren't bound but have an ancestor that is.

So the answer is that there's no foolproof method.

-- Michael

Hasen el Judy

unread,
Apr 19, 2013, 12:50:44 PM4/19/13
to knock...@googlegroups.com
cool, then you can make this "foolproof" if you iterate on an elements parent until you reach the body element.

Michael Best

unread,
Apr 19, 2013, 4:37:21 PM4/19/13
to knock...@googlegroups.com
No, that what ko.contextFor does already, which is why it's not foolproof.

-- Michael
Reply all
Reply to author
Forward
0 new messages