繁体   English   中英

灰烬-绑定嵌套数组

[英]Ember - binding nested arrays

我有一个Ember组件,它以数组的内容作为内容,如果它是一个对象,则每个项目都会呈现一个复选框;如果是数组,则为每个嵌套实例渲染一个新的嵌套实例。

该组件必须能够将数据绑定到根数组中的第一组项目以及所有嵌套项目。 我虽然使用计算属性和观察者,但按照Ember的规定, You cannot use nested forms like todos.@each.owner.name or todos.@each.owner.@each.name.

知道如何实现吗?

零件:

<dl class="selection-list">
    {{#each content}}
        <dd>
            {{#isArray this}}
                {{selection-list content=this}}
            {{else}}
                <label class="checkbox">
                    {{input type="checkbox" checked=selected disabled=disabled}}
                    {{label}}
                </label>

                {{#isArray children}}
                    {{selection-list content=children}}
                {{/isArray}}
            {{/isArray}}
        </dd>
    {{/each}}
</dl>

模型:

App.ApplicationRoute = Ember.Route.extend({
  model: function () {
    return {
      items: [
        {
          label: '1'
        },
        {
          label: '2', 
          children: [
            {
              label: '2.1'
            }, 
            {
              label: '2.2',
              children: [
                {
                  label: '2.2.1'
                },
                {
                  label: '2.2.2'
                },
                {
                  label: '2.2.3'
                }
              ]
            }
          ]
        },
        {
          label: '3'
        },
        {
          label: '4'
        }
      ]
    };
  }
});

使用绑定:

App.ApplicationController = Ember.ObjectController.extend({
    selectionCount: function () {
       return this.get('items').filterBy('selected').length;
    }.property('items.@each.selected')
});

例:
http://jsbin.com/yipuw/1/edit?html,js,output

我使用视图解决了这个问题。 关键点是:

  • 将组件更改为视图。 我们要为N个嵌套层保留相同的上下文。
  • 使用了Ember.Checkbox并在那里处理了change事件,因此我们可以在单击发生时修改selectCount ,而不是每次更改其中一个复选框时都对其进行遍历。

签出: http : //jsbin.com/nuzex/1/edit?html,js,console,output

我真的不觉得我们到目前为止还没有解决嵌套数组绑定问题的“解决方案”。 我已经用我自己当前的工作解决方案扩展了您的垃圾箱。 这样做的关键是伪财产,其人生的唯一任务是通知父母家谱中某处发生了变化。

http://jsbin.com/yivif/2/edit?html,js,output

其实,这是一个稍紧的版本

http://jsbin.com/yivif/10/edit?html,js,output

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM