【问题标题】:Ember.js - how do I bind a controller in a nested collection?Ember.js - 如何在嵌套集合中绑定控制器?
【发布时间】:2012-07-14 13:31:53
【问题描述】:

我正在学习 ember.js,我还没有用它构建任何东西。

我将数据存储在模型对象中,并使用控制器充当视图和模型之间的粘合剂。控制器将内容设置为模型的实例,并且视图绑定到控制器中的内容(因此通过代理绑定到模型)。像这样:

// js
App.MyModel = Ember.Object.extend({foo:''});
App.myController = Ember.Object.create({
    content: App.MyModel.create()
});

// html
{{view Ember.TextInput valueBinding="App.myController"}}

到目前为止一切顺利。但我不知道如何将此范式应用于嵌套集合:

//js
App.ChildController = Ember.ArrayController.extend();
App.NestedModel = Ember.Object.extend({
    init: function() {
        this._super();
        this.set('children', []);
        // Here: I can't give a global name for the content binding, and I don't know how to give a relative one
        this.set('childController', App.ChildController.create({contentBinding: 'children');
    }
});
App.myController = Ember.ArrayController.create({
    content:[],
    newChild: function() {
        this.pushObject(App.NestedModel.create());
    }
});


// html
{{#collection contentBinding="App.myController"}}
    {{#collection contentBinding="content.childController"}} <!-- nope -->
        {{content.childField}}
    {{/collection}}
{{/collection}}

这里有一些你可以摆弄的东西:http://jsfiddle.net/DwheG/

我要问的是:

  1. 我的建模是否正确?
  2. 如何绑定子控制器的内容?我必须使用字符串吗?传入对象 (this) 对我不起作用。 Ember 的路径解析算法是否记录在任何地方?
  3. 如何将嵌套集合助手绑定到嵌套控制器?

【问题讨论】:

  • haha stackoverflow 删除了我问题开头的“你好”... hvgotcodes 删除了“世界”:)

标签: ember.js


【解决方案1】:

我阅读了源代码并确认绑定只能使用路径指定。

Ember 使用getPath(root, path) 来解析路径。 root 是开始查找的对象,路径是字符串。如果路径是全局的,则可以省略根元素(在这种情况下,根元素将设置为window)。因此,例如,getPath(myController, 'foo.bar') 在评估时将返回 myController.foo.bar

所以:鉴于我需要以某种方式引用我的数组的包含对象以创建绑定,并且没有内置工具来执行此操作,我只是在我的模型中添加了对包含对象的引用.见这里:http://jsfiddle.net/CP448/1/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-09
    • 2012-04-20
    • 2013-02-23
    • 2013-03-11
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多