Domanda

I have an Ember.js route with a renderTemplate that allows the controller/template to be set by the model. I'm able to get the new controller to function, but when the inherited methods are called, it throws errors about not having a reference to the model that should have been supplied by the model method in the route. Here is the route:

App.ActivityRoute = App.GameScreenRoute.extend({
    model: function(params) {
        var lessons = App.getLessons(),
            lessonId = params.lesson_id,
            activityId = params.activity_id,
            lesson = lessons[lessonId],
            activity = lesson ? lesson.activities[activityId] : null;

        if(activity) {
            return activity.load();
        }
        else {
            return Promise.reject();
        }
    },

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

When model.controller === undefined, the controller used is ActivityController and it's supplied with the model returned by the model method, but if I set model.controller to another controller, the different controller doesn't have a reference to the model returned by the model method.

Is there something I'm missing or not understanding? Any help would be greatly appreciated.

È stato utile?

Soluzione

This is because default setupController hook, which is invoked for ActivityRoute sets the model only for the ActivityController.

I believe by overriding setupController hook you will be able to achieve required behavior:

setupController: function(controller, model) {
    this.controllerFor(model.get("controller") || "activity").set('model', model);
}

I'm not 100% sure if it will work, the controllers must be instantiated somewhere in order to be provided by model. But I guess you have already taken care about that.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top