gpt4 book ai didi

backbone.js - Requirejs 环境中 Backbone.js ajax 请求的全局错误处理程序

转载 作者:行者123 更新时间:2023-12-01 23:55:52 26 4
gpt4 key购买 nike

define(function (require) {

"use strict";

var $ = require('jquery'),
Backbone = require('backbone'),
Shell = require('app/views/Frame/Shell'),
Auth = require('app/models/json/session');

// Tell jQuery to watch for any 401 or 403 errors and handle them appropriately
$.ajaxSetup({
statusCode: {
401: function(){
// Redirect the to the login page.
Backbone.history.navigate("login", true);
},
403: function() {
// 403 -- Access denied
Backbone.history.navigate("login", true);
}
}
});

return Backbone.Router.extend({

routes: {
"" : "home",
"login" : "login",
"logout" : "logout"
},

initialize: function (opt) {
ev = opt.ev;
//Session.fetch();
},

home: function (id) {
new Dashboard({ev: ev});
}

});
});

上面的结构我是从http://clintberry.com/2012/backbone-js-apps-authentication-tutorial/学来的我觉得像这样全局设置 ajax 错误不是正确的方法

backbone 和 requirejs 的正确使用方法是什么?

最佳答案

define(["Backbone"], function(Backbone){
Backbone.ajax = function() {
// Invoke $.ajaxSetup in the context of Backbone.$
Backbone.$.ajaxSetup.call(Backbone.$, {
statusCode: {
401: function(){
// Redirect the to the login page.
Backbone.history.navigate("login", true);
},
403: function() {
// 403 -- Access denied
Backbone.history.navigate("login", true);
}
}
});
return Backbone.$.ajax.apply(Backbone.$, arguments);
};
});

关于backbone.js - Requirejs 环境中 Backbone.js ajax 请求的全局错误处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23852237/

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