2
votes

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?

2

2 Answers

2
votes

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"
        });
    }
});
0
votes

Use this.controllerFor('item') to specify a particular controller to the template and you must define that controller.