Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js 是否可以将视图的控制器切换到其他对象_Ember.js - Fatal编程技术网

Ember.js 是否可以将视图的控制器切换到其他对象

Ember.js 是否可以将视图的控制器切换到其他对象,ember.js,Ember.js,我想对基于参数的视图使用自定义控制器。管制员不是路线链中的第一个,因此我认为我不能使用“需求”。这是我尝试过的,但对我来说效果不好 App.MyBasicsView = Ember.View.extend({ //TRY 1: failed initialize: function () { var someParam = get some param from default controller;//failed controller is not set

我想对基于参数的视图使用自定义控制器。管制员不是路线链中的第一个,因此我认为我不能使用“需求”。这是我尝试过的,但对我来说效果不好

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')

...
}

在这种情况下,最好使用
Ember.Component
而不是
Ember.View
并从模板传递模型

例如:

someParam是控制器中的属性


好的,我们在路由中使用了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版本中,将tbe与助手一起使用会更简单,如下所示:

{{#with model controller="myDesiredController}}
.......
{{/with}}

是的,我也考虑过这个问题,它肯定是解决方案之一,但我必须使用基于“param”的单独组件。我的基本视图对应于一个路由,所以我必须在视图中嵌入这个动态组件,我尝试通过执行Ember.handlebar.compile(“我的参数特定视图”)来设置视图的模板,我运气不好,因为组件扩展了视图,我想我也会遇到同样的问题。不过我会再试一次。谢谢。我只是想听听你的意见,如果我用渲染助手而不是组件呢?
{{#with model controller="myDesiredController}}
.......
{{/with}}