Knockoutjs:动态内容和 applyBindings

发布于 2024-12-23 14:41:09 字数 1097 浏览 0 评论 0原文

我像这样“动态”填充我的页面:

<script type="text/html" id="ContainerTemplate">
  <span data-bind="template: {
                     name: contentTemplate,
                     data: contentData }"></span>
</script>

<script type="text/html" id="fooTemplate">
  <span data-bind="text: barAttribute"></span>
</script>

<button data-bind="click: complete">complete</button>

Hello
<span data-bind="template: { name: 'ContainerTemplate', foreach: myContents }"></span>
!

ViewModel:

var viewModel = {
    myContents: ko.observableArray([]),
    complete: function() {
        viewModel.myContents.push({
            contentTemplate:'fooTemplate',
            contentData:{barAttribute:'world'}});
    }
};

ko.applyBindings(viewModel);

一个特殊之处是模板名称是动态的。它似乎是这样工作的(您可以在 http://jsfiddle.net/hPQNx/ 上尝试),但我想知道我做事是否正确。某些模板功能(例如根或父级)似乎不起作用。

我应该在某个时候手动重新调用 applyBindings 吗?我已经看到这必须在相关的 DOM 节点上完成,但是如何在我的设置中访问这些节点?

I am "dynamically" populating my page like this:

<script type="text/html" id="ContainerTemplate">
  <span data-bind="template: {
                     name: contentTemplate,
                     data: contentData }"></span>
</script>

<script type="text/html" id="fooTemplate">
  <span data-bind="text: barAttribute"></span>
</script>

<button data-bind="click: complete">complete</button>

Hello
<span data-bind="template: { name: 'ContainerTemplate', foreach: myContents }"></span>
!

ViewModel:

var viewModel = {
    myContents: ko.observableArray([]),
    complete: function() {
        viewModel.myContents.push({
            contentTemplate:'fooTemplate',
            contentData:{barAttribute:'world'}});
    }
};

ko.applyBindings(viewModel);

A particularity is that template names are dynamic. It seems to work like this (you can try it on http://jsfiddle.net/hPQNx/ ), but I wonder if I'm doing things correctly. Some template features like root or parent don't seem to be working.

Should I manually re-call applyBindings at some point ? I have seen this must be done on the related DOM nodes, but how can I access those nodes in my setup ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渡你暖光 2024-12-30 14:41:09

我向您的视图模型添加了一个属性,并展示了如何添加根属性并使用 $root 引用它,并且 $parent 可以在这个小提琴中工作。

var viewModel = {
    a: ko.observable('foo'),
    myContents: ko.observableArray([]),
    complete: function() {
        viewModel.myContents.push({
            contentTemplate: 'fooTemplate',
            b: 'goo',
            contentData: {
                barAttribute: 'world'
            }
        });
    }
};

ko.applyBindings(viewModel);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/2.0.0/knockout-min.js"></script>
<script type="text/html" id="ContainerTemplate">
  <span data-bind="template: {
                     name: contentTemplate,
                     data: contentData }"></span>
</script>
      
<script type="text/html" id="fooTemplate">
  <span data-bind="text: barAttribute"></span>
  <div data-bind="text: $root.a"></div>
  <div data-bind="text: $parent.b"></div>
</script>

<button data-bind="click: complete">complete</button>

Hello
<span data-bind="template: { name: 'ContainerTemplate', foreach: myContents }"></span>
!

I added a property to your view model and showed how you can add a root property and reference it with $root and $parent can work here in this fiddle.

var viewModel = {
    a: ko.observable('foo'),
    myContents: ko.observableArray([]),
    complete: function() {
        viewModel.myContents.push({
            contentTemplate: 'fooTemplate',
            b: 'goo',
            contentData: {
                barAttribute: 'world'
            }
        });
    }
};

ko.applyBindings(viewModel);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/2.0.0/knockout-min.js"></script>
<script type="text/html" id="ContainerTemplate">
  <span data-bind="template: {
                     name: contentTemplate,
                     data: contentData }"></span>
</script>
      
<script type="text/html" id="fooTemplate">
  <span data-bind="text: barAttribute"></span>
  <div data-bind="text: $root.a"></div>
  <div data-bind="text: $parent.b"></div>
</script>

<button data-bind="click: complete">complete</button>

Hello
<span data-bind="template: { name: 'ContainerTemplate', foreach: myContents }"></span>
!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文