rotundasoftware / backbone.collectionView

Easily render backbone.js collections. In addition to managing model views, this class supports automatic selection of models in response to clicks, reordering models via drag and drop, and more.
http://rotundasoftware.github.io/backbone.collectionView/
Other
171 stars 27 forks source link

Bunch of selection events fires when a collection is re-fetched #68

Closed graulund closed 8 years ago

graulund commented 8 years ago

This is not an entirely objective point of view, so I understand if you won't fix it.

When I have a collection in the view that I reload completely from the server, the following line in the code causes the collectionView to fire as many "selectionChanged" events as there are items in the list, resulting in a ton of event calls every time the list is reloaded, events that could be mistaken as legit user clicks in the list.

https://github.com/rotundasoftware/backbone.collectionView/blob/master/src/backbone.collectionView.js#L610

I appreciate how this line works when only one or two elements are removed, however I propose that a way is needed to disable this line in the code, or to do something that works around the consequences when the entire collection is reloaded.

Thanks! :)

dgbeck commented 8 years ago

Hi @graulund ! Thanks for your feedback. Yes, it seems like something is amiss here and that we should be able to fix it.

Can you please specify how exactly the collection is being reloaded from the server?

graulund commented 8 years ago

I was simply doing it using a fetch() on the collection. :)

dgbeck commented 8 years ago

Hi Andy,

What version of backbone were you using?

Thx!

On Tue, Dec 8, 2015 at 2:44 AM, Andy Graulund notifications@github.com wrote:

I was simply doing it using a fetch() on the collection. :)

— Reply to this email directly or view it on GitHub https://github.com/rotundasoftware/backbone.collectionView/issues/68#issuecomment-162846111 .

graulund commented 8 years ago

Newest version. 1.2.3.

dgbeck commented 8 years ago

Hi Andy,

Looks like the default fetch implementation calls set, which ends up firing a remove event for each model that is removed, and can cause those selectionChanged events to get fired. That seems like expected behavior to me from the perspective of the collection view.. the weird part is in backbone. Fortunately, from looking at the backbone fetch code,

fetch: function(options) {
      options = _.extend({parse: true}, options);
      var success = options.success;
      var collection = this;
      options.success = function(resp) {
        var method = options.reset ? 'reset' : 'set';
        collection[method](resp, options);
        if (success) success.call(options.context, collection, resp, options);
        collection.trigger('sync', collection, resp, options);
      };
      wrapError(this, options);
      return this.sync('read', this, options);
    },

I think you should be able to work around the issue by passing { reset : true } to as an option to fetch (or { silent : true }, but then you would need to explicitly re-render the collection view).

Closing for now. Let me know if that does not work for your needs!

Thx!