मुझे सर्वर से सभी प्रतिक्रियाओं में एक संभावित लॉगिन पेज पकड़ने की आवश्यकता है, इसलिए मैंने वैश्विक रूप से 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 वादों के साथ प्रयास किया लेकिन मुझे त्रुटि स्थिति से सफलता (जब मूल Success को कॉल करते समय) स्विच करने में सक्षम होना चाहिए, जो वादे के साथ काम नहीं कर रहा था।
क्या आप अपना jQuery प्रयास दिखा सकते हैं? यह वादा [एसआईसी] लगता है। –