gpt4 book ai didi

javascript - 错误子状态

转载 作者:行者123 更新时间:2023-12-03 08:57:18 28 4
gpt4 key购买 nike

当嵌套路由中发生错误时,抛出的错误会冒泡到父路由,直到其中一个捕获它。
可以处理错误的根级别是Application的。但是,setupController ApplicationRoute 的钩子(Hook)从 ErrorRoute 转换后触发与 undefined语境。
代码示例可以在这里查看: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://");
}
});

最佳答案

路线从不“有”模型,它只有一个模型钩子(Hook),用于获取路线的模型。当转换到错误时,不会尝试解决任何模型(如果出现错误,您将处于无限循环中)。

如果您的模型是静态内容,您可以在 setupController 中设置它。

关于javascript - 错误子状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20576508/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com