这种情况是我希望我的{{render}}
从提供的余烬数据模型的名称自动推断出控制器名称。
例如在{{render 'animal' animal}}
中,如果animal
是Cat
,我希望模板使用CatController
似乎渲染帮助器现在接受controller=
参数,但随后将该参数视为文字而不是变量。
目前,我只是将表示形式的代码(模板需要)放在目标模型中,我认为这是不好的。
实现这种类似多态的渲染的正确方法是什么?
您正在描述的功能正是余烬的用途,如此处http://emberjs.com/guides/templates/rendering-with-helpers所述
{{render 'animal' animal}}
将使用动物模型,AnimalController控制器和AnimalView视图(如果存在)渲染动物模板。
您需要使用渲染助手的替代方法,您可以在其中指定所需的所有内容。
this.render('TEMPLATENAME', {
into: 'TEMPLATE/LAYOUT',
outlet: 'OUTLET NAME IN TEMPLATE',
controller : 'CONTROLLER NAME FOR CURRENT TEMPLATE', /* optional */
model : 'MODEL NAME FOR CURRENT TEMPLATE', /* optional */
});
您可以在renderTemplate函数中直接从您的路线添加它>
App.ApplicationRoute = Ember.Route.extend({
model: function(params){ },
renderTemplate: function(){
this._super(); /* Add it to prevent weird behaviour */
this.render('header', {
into: 'application',
outlet: 'header',
controller: 'headerMenu',
)};
},
});