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
Post a Comment