【发布时间】:2013-12-13 21:42:38
【问题描述】:
当嵌套路由中发生错误时,抛出的错误会冒泡到父路由,直到其中一个捕获它。
可以处理错误的根级别是Application's。但是,ApplicationRoute 的 setupController 钩子是在使用 undefined 上下文从 ErrorRoute 转换后触发的。
代码示例可以在这里查看:http://jsbin.com/ucanam/2563/edit(在控制台中给出输出)
在ApplicationRoute 中,我指望它的初始模型在从ErrorRoute 转换后出现。为什么它被重置为undefined,否则我该如何解决?
编辑:
以下是一些相关代码:
App = Ember.Application.create({});
App.Router.map(function() {
this.route('articles');
});
App.ApplicationRoute = Ember.Route.extend({
model: function() {
/* model hook is not being called when the error bubbles up (nor afterModel hook) */
/* This is the model expected in Application level */
return [
{"key1": "val1"},
{"key2": "val2"},
{"key3": "val3"}
];
},
setupController: function(controller, model) {
/* This hook is called in the usual scenario, when ApplicationRoute first
invoked, with same model as defined in the model hook */
/* However, it is also invoked after an error thrown from Articles Route,
what happens then is:
typeof model == "undefined" */
console.log("SetupController invoked in Application Route:", model);
}
});
App.ArticlesRoute = Ember.Route.extend({
model: function() {
/* The following throws the error */
Ember.$.ajax("http://");
}
});
【问题讨论】:
-
链接不错,但在正文中也要提供相关代码。
标签: javascript ember.js error-handling ember-router