New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dataFor / contextFor not honoring controlsDescendantBindings #2148
Conversation
This is really a support question, as it looks like there are ways to achieve the desired outcome without modifying knockout – I suggest you pop over to gitter.im/knockout or ask on Stackoverflow. I will close the issue on that basis, but if there is room for an improvement please post and we can re-open. Cheers |
i can't disagree with your statement that there are potential ways to know that the binding has occurred since i clearly don't know how utilizing knockout alone, but are you saying that the described behavior of thanks |
Knockout currently doesn't maintain a foolproof way of determining if an element has been bound or not. We can know that an element has been bound, but not that it hasn't been bound. In other words, it's possible that Knockout will treat an element that has been bound as if it hasn't. |
appreciate the input. As it relates to the behavior of those two mentioned functions and check out this other explanation of the behavior i'm describing. specifically in the comments:
|
Given an example like this (where <div id="1" data-bind="visible: isVisible">
<span id="2">Some text</span>
<div id="3" data-bind="allowBindings: false">
<input id="4" data-bind="value: someValue">
</div>
</div> Knockout will know that elements 1 and 3 were bound, but it will think that element 2 was not bound because it has no bindings. Currently Knockout marks an element as bound only if it has a binding. |
thanks for the link. so to be clear, the issue that i'm describing is that given this example, if i were to do |
and if you take a look at the console here, you'll see your example with a log showing what i'm talking about. i would expect that |
You'd expect a null when the value set in the 4's data-bind="value: someValue" was non-null? If someValue came from bound data, why wouldn't dataFor() return the data? |
hey there, welcome to the party! lol. so i'm expecting null due to the |
Ok, thanks for the warm welcome :) I guess the way I'd interpret this is that the allowBindigns thing doesn't remove the context, and when you do dataFor() I am guessing it pulls the $data from the active context, which has been set at a higher level. So, if that's the case, maybe the allowBindings would need to reset the context to a new context with an empty $data. |
right so the trick that the per what you're saying, the active context would then be completely different after this second |
Right, If i'm undersatnding the allowBindings custom binding, it's setting controlDescendtantBindings: true/false depending on the observable value that's being passed into the allowBindings databind. This custom binding by itself doesn't establish a new context (i'm making this assumption, but this is what I expect) so therefore ko.forData() will find whatever the parent context is (I wouldn't call this a 'fake parent context' it's actualy the current context!). if you alter allowBindigns to reset the context to something empty, that would get what you want. after you call apply bindings, you'd have to do it on a new dom element that is under the element you put the allowBindings databinding on because the dom element with allowBindings on it has been databound to!. But any dom element you can find using a jquery selector or whatever under that element is fair game to bind to, and I believe that apply bindings will establish a new binding root level (meaning there will be no parent context of this context created by calling applyBindings(). that might be some other issue for you, i don't know your complete use case. |
totally. so i typically only utilize this at the end of the day, it feels like a bug to me that the parent data/context is present on an element that is itself wrapped in can any of you KO guys weigh in on if this is by design or perhaps a flaw? thanks! |
As I've explained, Knockout doesn't currently have any way to tell you that an element hasn't been bound. I'll reopen this as a request to add that feature. |
Could this be as simple as adding a Symbol to the DOM node? |
…en if they are descendants of a bound element.
This topic has been around a while. See #905. I've attached the fix. |
mrtristan:
so in my initial research, the common check to see if an element was bound that people do is something along the lines of
!!ko.dataFor(someElement)
... this at the surface seems like exactly what i need for my usecase, however there seems to be one issue: this does not take in to accountcontrolsDescendantBindings
. I have areas of my application that i need to separately bind and ideally i'd like to be able to ask ko if that has happened... i utilizecontrolsDescendantBindings
to power this separation of concerns and am looking for a solution to check ifapplyBindings
has occured anddataFor
seems to traverse up the DOM and look for some parent binding. i'm expectingnull
and getting data for an unbound div.thoughts? this seems like a deficiency in that
dataFor
/contextFor
logic.Thanks.