您可以拨打ko.applyBindings绑定的局部视图? [英] Can you call ko.applyBindings to bind a partial view?

查看:643
本文介绍了您可以拨打ko.applyBindings绑定的局部视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用KnockoutJS,有一个主视图,视图模型。我想一个对话框(在jQuery UI的一个),弹出与另一种观点认为其独立的子视图模型绑定到。

I'm using KnockoutJS and have a main view and view model. I want a dialog (the jQuery UI one) to popup with another view which a separate child view model to be bound to.

的HTML对话框的内容使用AJAX是retreived,所以我希望能够调用 ko.applyBindings 一旦请求已经完成了,我要绑定的孩子视图模型到HTML的那部分通过AJAX的对话框DIV中加载。

The HTML for the dialog content is retreived using AJAX so I want to be able to call ko.applyBindings once the request has completed, and I want to bind the child view model to just the portion of the HTML loaded via ajax inside the dialog div.

这实际上可能还是需要加载所有我的看法和观点模型当页面最初加载,然后调用 ko.applyBindings 一次?

Is this actually possible or do I need to load ALL my views and view models when the page initially loads and then call ko.applyBindings once?

推荐答案

ko.applyBindings 接受第二个参数是作为根使用DOM元素。

ko.applyBindings accepts a second parameter that is a DOM element to use as the root.

这会让你做这样的事情:

This would let you do something like:

<div id="one">
  <input data-bind="value: name" />
</div>

<div id="two">
  <input data-bind="value: name" />
</div>

<script type="text/javascript">
  var viewModelA = {
     name: ko.observable("Bob")
  };

  var viewModelB = {
     name: ko.observable("Ted")
  };

  ko.applyBindings(viewModelA, document.getElementById("one"));
  ko.applyBindings(viewModelB, document.getElementById("two"));
</script>

所以,你可以使用这个技术来绑定一个视图模型为您加载到您的对话框中的动态内容。总体而言,你只是要小心不要叫 applyBindings 多次在相同的元素,你将获得附加多个事件处理程序。

So, you can use this technique to bind a viewModel to the dynamic content that you load into your dialog. Overall, you just want to be careful not to call applyBindings multiple times on the same elements, as you will get multiple event handlers attached.

这篇关于您可以拨打ko.applyBindings绑定的局部视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆