javascript - Backbone global success handling -


in our project use backbone access rest service.
service delivers http header contains kind of session token authenticate current user.
token changes on every request.

we use token our login model determine if login need on current route.

the first idea using jquery.ajaxsuccess.

$(document).ajaxsuccess(function(event, xhr, settings) {     updatetoken(xhr); }); 

works global success triggered after success handlers in backbone.
if backbone success handler starts new request server, has old token @ moment , request rejected.

call updatetoken in every success handler or continuing processing using settimeout effort , error prone. may forgotten.

now created solution

var bbsync = backbone.sync; backbone.sync = function(method, model, options) {     var success = options.success;     options.success = function(model, response, options) {         updatetoken(options);         success(model, response, options);     };     return bbsync.call(this, method, model, options); }; 

all success methods wrapped.
there risk in solution?
have overlooked something?
better solutions?

i can confirm approach works same thing same reason.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -