简体   繁体   中英

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({});

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.

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