Jason
Jason

Reputation: 4149

Ember.js Route.setupController with ObjectController

I'm puzzled why how the Route and Controller are affecting the default Model. Here is an example.

App.ApplicationRoute = Ember.Route.extend({
    setupController: function(controller, model) {
        this._super(controller, model);
        console.log(model); //returns undefined
        controller.set("title", "Page title");
    }
});

This code snippet above works without errors; the template prints {{title}} as expected. Note that the model is "undefined."

App.ApplicationRoute = Ember.Route.extend({
    setupController: function(controller, model) {
        this._super(controller, model);
        console.log(model); //returns undefined
        controller.set("title", "Page title");
    }
});

App.ApplicationController = Ember.ObjectController.extend({});

The code immediately above throws an error ...

(Error while processing route: index Assertion Failed: Cannot delegate set('title', Page title) to the 'content' property of object proxy : its 'content' is undefined.)

... and yields a blank page. The solution is to return a model (blank object) or use a Controller (default behavior) instead of an ObjectController. Could someone explain this peculiar circumstance? Why doesn't Ember assume an empty object when using an ObjectController? Is it assuming the object will be passed in or retrieved from the store or server?

App.ApplicationRoute = Ember.Route.extend({
    model: function() {
        return {};
    },
    setupController: function(controller, model) {
        this._super(controller, model);
        console.log(model);
        controller.set("title", "Page title");
    }
});

App.ApplicationController = Ember.ObjectController.extend({});

Upvotes: 0

Views: 252

Answers (1)

quaertym
quaertym

Reputation: 3961

As stated in the Ember docs:

Ember.ObjectController is part of Ember's Controller layer. It is intended to wrap a single object, proxying unhandled attempts to get and set to the underlying model object, and to forward unhandled action attempts to its target.

ObjectController expects a model is present and it is set as the content. It is basically a wrapper around the single object.

Upvotes: 1

Related Questions