我想基于参数为视图使用自定义控制器。控制器不在路线链中,所以我不认为我可以使用"需要"。这就是我尝试过的,但对我来说效果不佳!
App.MyBasicsView = Ember.View.extend({
//TRY 1: failed
initialize: function () {
var someParam = get some param from default controller;//failed controller is not set at this instance
//based on someParam initialize a controller and set this to the view
}.on('init')
//TRY 2 : no errors but the template doesn't render
setController: function(){
var container = this.get("controller.container"),
model = this.get("controller.model"),
controller = MY-CUSTOM-CONTROLLER.create({'container': container, 'model': model});
this.set('controller', controller);
}.observes('controller.someParam')
...
}
答案 0 :(得分:1)
在这种情况下,最好使用Ember.Component
而不是Ember.View
并从模板传递模型
例如:
someParam是控制器中的属性
{{my-basics myBasicProperty=someParam}}
答案 1 :(得分:0)
好的,我们最终在路线中使用了rederTemplate挂钩:
renderTemplate: function (controller, model) {
var questionType = some-type,
controllerName = (questionType.classify())+"BasicsController",
template = 'views.questions.basics.' + questionType + "-question",
typeBasedController = controller;
//if cannot resolve the template then use the default template
template = (this.container.resolve('template:' + template)) ? template : 'views.questions.basics.default-question';
var factory = this.container.lookupFactory('controller:' + controllerName);
if (factory) {
typeBasedController = factory.create({'model': model });
}
this.render(template, {
'controller': typeBasedController
});
}
它基本上让我们使用控制器和我们想要的视图。现在在最新的ember版本中,使用像这样的帮助器更简单:
{{#with model controller="myDesiredController}}
.......
{{/with}}