我需要捕捉来自服务器的所有响应中可能的登录页面,所以我已全局覆盖Backbone.sync,以便在传递它们之前检查所有错误。捕捉骨干同步错误
Backbone.originalSync = Backbone.sync;
Backbone.sync = function (method, model, options) {
var originalSuccess, originalError;
console.log("sync override...");
// remember original success so we can call it if user logs in successfully
originalSuccess = options.success;
// proxy the error callback to first check if we get a login page back
originalError = options.error;
options.error = function (model, xhr, options) {
if (xhr.status === 200 && xhr.responseText === "") {
// parse error from empty response (jq1.9 invalid json, ok)
originalSuccess(model, xhr, options);
} else {
console.log("Sync error " + statusTxt + ", " + thrown.message);
if (xhr.status === 200 || xhr.status === 302 || xhr.status === 0) {
// login page returned instead of json...
// open a new window with relogon.html to trigger a new login
window.showModalDialog("../relogon.html");
} else {
// normal error, pass along
if (originalError) {
originalError(model, xhr, options);
}
}
}
};
// call the original sync
Backbone.originalSync(method, model, options);
};
当从0.9.9变为1.0时,这个数字变得非常糟糕。看起来像原来的Backbone.sync以不同的方式包装它的错误处理程序,导致我的错误处理程序首先被调用,并带有jquery xhr签名。 我只好错误处理程序的签名改成这样:
options.error = function (xhr, statusTxt, thrown) {
确定,所以现在它的工作原理,但我得到,我做错了什么的感觉。
有没有更好的方法来做到这一点?
我试过jQuery的承诺,但我需要能够从错误状态切换到成功(当调用originalSuccess时),这似乎没有与承诺一起工作。
你可以显示你的jQuery尝试?这听起来很有希望[原文如此]。 – 2013-05-10 10:50:47