won*_*all 5 ember.js ember-data
什么是服务器的ember-data当前默认预期响应,如果它有200以外的东西,那么它不会抛出未捕获的异常,而是解析错误以供以后使用?
例如{"errors":{jsonerror}} ???
我应该如何处理Ember中的服务器错误响应(json格式)?
谢谢!
int*_*xel 16
@colymba得到了它几乎是正确的,但我会尝试做出更详细的答案,这样可以帮助您更轻松地起床和跑步.
正如在新的路由器改版中已经说明的那样,当尝试转换到路线,任何钩子beforeModel,model并且afterModel可能抛出错误,或者返回拒绝的承诺时,在这种情况下,error将在部分输入的路线上触发事件,允许您在每个路由的基础上处理错误.
App.SomeRoute = Ember.Route.extend({
model: function() {
//assuming the model hook rejects due to an error from your backend
},
events: {
// then this hook will be fired with the error and most importantly a Transition
// object which you can use to retry the transition after you handled the error
error: function(error, transition) {
// handle the error
console.log(error.message);
// retry the transition
transition.retry();
}
});
Run Code Online (Sandbox Code Playgroud)
如果您希望在应用程序范围内处理错误,您可以error通过覆盖处理程序来定义您自己的全局默认处理error程序ApplicationRoute,这可能如下所示:
App.ApplicationRoute = Ember.Route.extend({
events: {
error: function(error, transition) {
// handle the error
console.log(error.message);
}
}
});
Run Code Online (Sandbox Code Playgroud)
考虑到默认情况下,一旦在events散列上定义的处理程序处理它就会停止冒泡.但是要继续冒泡事件一直到ApplicationRoute你应该true从那个处理程序返回,所以它可以通知更多的error处理程序,然后你可以从中重新尝试转换到路径transition.retry().
自从新的ember 1.0版以来,events哈希被重命名为actions.路由的错误处理程序actions现在应该在哈希中:
App.ApplicationRoute = Ember.Route.extend({
actions: {
error: function(error, transition) {
// handle the error
console.log(error.message);
}
}
});
Run Code Online (Sandbox Code Playgroud)
希望这对你有所帮助.