Knockout.js:更新绑定?
当我在 ko.applyBindings(); 之后将任何新元素注入到 DOM 中时被调用,那么淘汰赛将无法识别这些新元素。 我可以理解为什么会发生这种情况 - 它们只是没有被淘汰索引。
因此,起初我认为在添加新元素后再次调用 ko.applyBindings() 即可解决此问题,但后来我意识到,对于您进行的每个 ko.applyBindings() 调用,相应的事件都会被多次触发。因此,应用五次后, click: 绑定将被触发五次,所以这不是一个理想的解决方案;)
是否有类似 ko.updateBindings() 或其他东西的东西,告诉淘汰赛,嗯......更新元素绑定?
问候, 克里斯
when I inject any new elements into the DOM after ko.applyBindings(); was called, then knockout won't recognize these new elements.
I can understand why this is happening - they are just not indexed by knockout.
So, at first I thought this would be solved by just calling ko.applyBindings() again, after adding my new elements, BUT then I realized that for every ko.applyBindings() call you make, the according events get fired multiple times. So after applying five times, a click: binding will be fired five times, so this is not a desireable solution ;)
Is there anything like ko.updateBindings() or something else, to tell knockout to, well... update the element bindings?
greetings,
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每次调用 ko.applyBindings 时,都会检查整个 DOM 的绑定情况。因此,如果多次执行此操作,您将为每个元素获得多个绑定。如果您只想绑定一个新的 DOM 元素,您可以将此元素作为参数传递给
applyBindings
函数:请参阅此相关问题:
可以调用ko.applyBindings来绑定分部视图吗?
Each time you invoke
ko.applyBindings
the entire DOM is inspected for bindings. As a result you will get multiple bindings for each element if you do this more than once. If you just want to bind a new DOM element you can pass this element as a parameter to theapplyBindings
function:See this related question:
Can you call ko.applyBindings to bind a partial view?
如果不知道你到底在做什么,你似乎走错了路。您的视图应该由视图模型驱动。因此,您不应该直接添加随后需要应用剔除绑定的 DOM 元素。
相反,您应该更新视图模型以反映视图中的更改,然后导致新元素出现。
例如,对于您的
$('body').append('Click me!');< /code>,而不是在按钮应该可见时添加 DOM 元素,而是使用视图模型控制按钮可见性。
因此,您的视图模型包括
并且您的 HTML 包括
当应用程序状态发生变化,因此“单击我”按钮可用时,您只需
viewModel.clickMeAvailable(true)
即可。这样做的目的,也是淘汰赛的一个重要部分,是将业务逻辑与表示分离。因此,使“click me”可用的代码并不关心“click me”涉及一个按钮。它所做的只是在“click me”可用时更新
viewModel.clickMeAvailable
。例如,假设“单击我”是一个保存按钮,在有效填写表单后应该可用。您可以将保存按钮可见性与
formValid
视图模型可观察对象联系起来。但随后您决定更改一些内容,因此在表格生效后,会出现一份法律协议,在保存之前必须先同意该协议。表单的逻辑不会改变 - 当表单有效时它仍然设置
formValid
。您只需更改formValid
更改时发生的情况即可。正如 lassombra 在这个答案的评论中指出的那样,在某些情况下,直接 DOM 操作可能是您的最佳方法 - 例如,在复杂的动态页面中,您只想在需要时对视图的部分进行水合。但这样做你就放弃了 Knockout 提供的一些关注点分离。如果您正在考虑进行这种权衡,请务必小心。
Without knowing what you're up to exactly, it seems like you're going the wrong way about this. Your view should be driven by your view model. So you shouldn't be directly adding DOM elements you then need to apply knockout bindings to.
Instead you should be updating your view model to reflect the change in the view, which then causes your new element to appear.
So for example, for your
$('body').append('<a href="#" data-bind="click: something">Click me!</a>');
, rather than adding the DOM element when the button should be visible, control the button visibility using the view model.So your view model includes
And your HTML includes
When the application state changes so the click me button is available, you then just
viewModel.clickMeAvailable(true)
.The point of doing this, and a big part of knockout, is to separate business logic from presentation. So the code that makes click me available doesn't care that click me involves a button. All it does is update
viewModel.clickMeAvailable
when click me is available.For example, say click me is a save button that should be available when a form is filled in validly. You'd tie the save button visibility to a
formValid
view model observable.But then you decide to change things so after the form is valid, a legal agreement appears which has to be consented to before saving. The logic of your form doesn't change - it still sets
formValid
when the form is valid. You would just change what occurs whenformValid
changes.As lassombra points out in the comments on this answer, there are cases when direct DOM manipulation may be your best approach - for example a complex dynamic page where you only want to hydrate parts of the view as they are needed. But you are giving up some of the separation of concerns Knockout provides by doing this. Be mindful if you are considering making this trade-off.
我刚刚偶然发现了类似的问题。我尝试向容器添加新元素并为它们提供 onclick 函数。
起初尝试了你所做的事情,甚至尝试了方法 ColinE 推荐。这对我来说不是一个实用的解决方案,所以我尝试了 SamStephens 方法并想出了这个方法,这对我来说非常有效:
HTML:
JavaScript:
您可以在 JS 中使用它小提琴是我做的。
I just stumbled upon a similar problem. I tried to add new elements to container and give those a onclick function.
At first tried the things you did, and even tried the approach ColinE recommended. This wasn't a practical solution for me so I tried SamStephens approach and came up with that, which works perfectly for me:
HTML:
JavaScript:
You can play with it in the JS Fiddle I made.