jquery - How make $.ajaxSetup rely on a backbone.js model initialized later -


i have following backbone.js application initialization process :

var app = {   init: function(arg) {     $.when(app.loadstaticfiles('any_file_to_load_before'))     .done(function(args) {        app.models.current_user = app.models.user();      });     // ...      // initializing backbone app , models     // ...     $.ajaxsetup({        headers: {authorization: 'token token=' + app.models.current_user.get('token')}     })   }     } $(document).ready(   function(){     app.init(args);   }; ); 

then when application initialize $.ajaxsetup evaluates app.models.current_user.get('token') sadly not defined.

i guess there missconceptions in design, how should rewrite code in order make work ?

i think main problem evaluation of $.ajaxsetup() before $.deferred resolution.

deferreds meant control order of application's processes. reading question, seems application's order needs be:

  1. app.loadstaticfiles('any_file_to_load_before')
  2. app.models.current_user = app.models.user();
  3. $.ajaxsetup(...)
  4. initializing backbone app , models?

you write using deferreds as:

var app = {   init: function(arg) {     $.when(app.loadstaticfiles('any_file_to_load_before'))       .done(function(args) {         app.models.current_user = app.models.user();       })       .done(function() {         $.ajaxsetup({           headers: {authorization: 'token token=' + app.models.current_user.get('token')}         })       })       .done(function() {         // ...          // initializing backbone app , models         // ...       });    } }; 

a tiny detail note has jquery's implied ordering of done() callbacks. in code, doing 4 static steps. if needed specific ordering , adding .done() callbacks dynamically, might not sure 1 added before others. above example works because jquery runs .done() callbacks in order added. if didn't have assumption, have proxy/filter deferred @ each done step through jquery's .then() function instead. way each intermediate step have 1 , 1 done callback returned deferred next 1 attach 1 , 1 callback ... , on.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -