ajax - BackboneJS .ajaxStart() and .ajaxStop() while fetching collection -


i have backbonejs app fetch bunch of collections. want apply sort of loader indicate collection loading , user gets know happening. want use .ajaxstart() , .ajaxstop()-method. thinking this:

this.artistscollection.fetch( $(document).ajaxstart(function () {     console.log('ajax start');            $('.somediv').addclass('test'); }), $(document).ajaxstop(function () {     console.log('ajax stop');            // stop doing stuff }) ); 

issue first time trigger .fetch() console says ajax stop , class not applied!?!? second time trigger .fetch() works should , class gets applied. know whats issue?

please anyone?

you're passing returned result of adding 2 event handlers jquery parameters collection fetch method. backbone collection fetch method receives options object can include success callback (see documentation).

i think if move listeners out of method call should work expect:

// global ajax listeners $(document).ajaxstart(function () {     console.log('ajax start');             // stuff });  $(document).ajaxstop(function () {     console.log('ajax stop');            // stop doing stuff });  this.artistscollection.fetch(); 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -