简体   繁体   English

Ember.js Route.setupController与ObjectController

[英]Ember.js Route.setupController with ObjectController

I'm puzzled why how the Route and Controller are affecting the default Model . 我不知道为什么RouteController如何影响默认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. 模板将按预期打印{{title}} 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.) (处理路由时发生错误:索引断言失败:无法将set('title',Page title)委派给对象代理的'content'属性:其'content'未定义。)

... and yields a blank page. ...并产生空白页。 The solution is to return a model (blank object) or use a Controller (default behavior) instead of an ObjectController . 解决方案是返回模型(空白对象)或使用Controller (默认行为)代替ObjectController Could someone explain this peculiar circumstance? 有人可以解释这种特殊情况吗? Why doesn't Ember assume an empty object when using an ObjectController ? 为什么Ember在使用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文档所述

Ember.ObjectController is part of Ember's Controller layer. Ember.ObjectController是Ember的Controller层的一部分。 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. ObjectController希望存在一个模型并将其设置为内容。 It is basically a wrapper around the single object. 它基本上是单个对象的包装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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