简体   繁体   中英

Variable controller in ember.js

I'm very new to using Ember.js, and this is confusing me greatly. Is there a way to use a variable controller when routing in ember.js?

I'll try to explain a bit:

I have a list of models that I need to be controlled by different controllers and represented by different templates. For example: If I have the route items/:item_id , and I want to do something like this:

App.ItemRoute = Ember.Route.extend({
    model: function(params) {
        return items[params.lesson_id]; // edit: should be item_id not lesson_id
    },

    setupController: function(controller, model) {
        controller.set("model", model);
    },

    renderTemplate: function(controller, model) {
        this.render(model.template || "item", {
            controller: model.controller || "ItemController"
        });
    }
});

The ember guides here: http://emberjs.com/guides/routing/rendering-a-template/ show the use of this.render with a controller option, but when I pass in anything (even "ItemController" ), I receive this error:

Error while loading route: Error: You passed controller: 'ItemController' into the render method, but no such controller could be found.

Is there a better way to do this?

You should be using the controller name, item , instead of ItemController and App.ItemController must be defined.

App.ItemRoute = Ember.Route.extend({
    model: function(params) {
        return items[params.lesson_id];
    },

    setupController: function(controller, model) {
        controller.set("model", model);
    },

    renderTemplate: function(controller, model) {
        this.render(model.template || "item", {
            controller: model.controller || "item"
        });
    }
});

使用this.controllerFor('item')为模板指定一个特定的控制器,您必须定义该控制器。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM