2012-07-13 80 views
3

我在学习ember.js,我还没有构建任何东西。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. 如何将嵌套收集帮助程序绑定到嵌套控制器?
+0

哈哈stackoverflow删除了“你好,”在我的问题开始...和hvgotcodes删除了“世界”:) – peets 2012-07-13 17:14:18

回答

0

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

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

因此:鉴于我需要以某种方式引用包含对象为我的数组创建绑定,并且没有内置设施来做到这一点,我只是添加了对我包含对象的引用模型。看到这里:http://jsfiddle.net/CP448/1/