BackboneJS before the fetch event

I have a collection (using Backbone.paginator) that retrieves an array of models from the server and returns to me. This collection is used on my "main view" and on "subviews". In each of these views, I have an on ("change") event, which in each view does a special thing. I thought if I could listen to some kind of "start fetch" event (similar to beforeLoad in jquery) to add a bootloader ger. Does Backbone provide one of these?

If not .. How to expand it?

Thanks!

+4
source share
4 answers

You can expand the prototype of the Backbone Collection as follows:

(function() { var fetch = Backbone.Collection.prototype.fetch; Backbone.Collection.prototype.fetch = function() { this.trigger('beforeFetch'); return fetch.apply(this, arguments); }; })(); 

Now you can do something like:

 myCollection.on('beforeFetch', function() { // take care of before fetch business }); 
+3
source

Just use the query event. It starts when the model (or collection) has launched a request to the server. It is available from version 0.9.9.

+16
source

You just need to add the call to some method that sets the loading screen before each call to the selection:

 this.collection.on('reset', this.displayNormal) this.displayLoader(); this.collection.fetch(); 

This should help you get started. If you need this event (I can understand why you did it), you can override the samples as follows

 fetch: function() { this.trigger('beforeFetch'); return Backbone.Collection.prototype.fetch.apply(this, arguments); } 

Hope this helps!

+2
source

What you can do is display the loaded image when you call fetch, and then set the success callback to the extract to hide it again.

0
source

Source: https://habr.com/ru/post/1437814/


All Articles