Javascript 带有ObjectController的Ember.js Route.setupController
我不明白为什么Javascript 带有ObjectController的Ember.js Route.setupController,javascript,ember.js,Javascript,Ember.js,我不明白为什么路线和控制器会影响默认的模型。这里有一个例子 App.ApplicationRoute = Ember.Route.extend({ setupController: function(controller, model) { this._super(controller, model); console.log(model); //returns undefined controller.set("title", "Page
路线
和控制器
会影响默认的模型
。这里有一个例子
App.ApplicationRoute = Ember.Route.extend({
setupController: function(controller, model) {
this._super(controller, model);
console.log(model); //returns undefined
controller.set("title", "Page title");
}
});
上面的代码片段可以正常工作,没有错误;模板按预期打印{{title}
。请注意,模型是“未定义的”
紧靠上方的代码抛出一个错误
(处理路由时出错:索引断言失败:无法委派。)
将('title',Page title)设置为对象代理的'content'属性
:其“内容”未定义。)
。。。并生成一个空白页。解决方案是返回模型(空白对象)或使用控制器
(默认行为),而不是对象控制器
。有人能解释一下这种特殊情况吗?为什么余烬在使用ObjectController
时不假定为空对象?是否假定对象将传入或从存储或服务器检索
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({});
如合同所述:
ObjectController是Ember控制器层的一部分。它是
用于包装单个对象,代理未处理的获取
并设置为基础模型对象,并设置为转发未处理的对象
行动试图达到目标
ObjectController要求存在一个模型,并将其设置为内容。它基本上是单个对象的包装器
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({});