答案 0 :(得分:1)
子视图将自动继承父视图的控制器,但是如果您需要将其与另一个控制器的实例相关联,则最好将其他控制器与父视图的控制器相关联。 例如,
http://emberjs.jsbin.com/kumaxihi/2/edit
App.IndexController = Ember.Controller.extend({
needs:["oneOther"],
prop1:"this is prop1",
prop2:"this is prop2",
prop3:"this is prop3"
});
App.IndexView = Ember.View.extend({
prop1OfController:Ember.computed.alias("controller.prop1"),
prop2OfController:function(){
return this.get("controller.prop2");
}.property()
});
App.RandomSubView = Ember.View.extend({
templateName:"random-sub-view",
prop3OfController:Ember.computed.alias("controller.prop3"),
thePropOfAnotherController:Ember.computed.alias("controller.controllers.oneOther.theProp")
});
App.OneOtherController = Ember.Controller.extend({
theProp:"this is the prop of the other controller"
});
但是,如果您想直接设置controller
属性,则可以执行this.set("controller",theControllerInstance)
之类的操作。现在控制器实例必须以某种方式可用,例如通过路由层次结构或父控制器的关联,甚至通过render
(http://emberjs.com/guides/routing/rendering-a-template/)的route
函数。< / p>