1

In order to keep update a Marionette.CompositeView (2) I implemented a long polling module which looks like the following (1).

I would like to make this polling smarter.
In detail I would like that this polling should not fetch the collection but check only the new elements to avoid rendering all the view again.
Any ideas?


(1)

define([
    'underscore'
], function (_) {
    "use strict";

    return {

        executePolling: function () {
            this.fetch({success : this.onFetch});
        },

        startPolling: function () {
            _.bindAll(this, 'onFetch', 'executePolling');
            this.minutes = 1;
            this.polling = true;
            this.executePolling();
        },

        stopPolling: function () {
            this.polling = false;
        },

        onFetch: function () {
            if (this.polling) {
                setTimeout(this.executePolling, 1000 * 60 * this.minutes);
            }
        }
    };

});

(2)

define([
    'underscore'
], function (_) {
    "use strict";

    return Marionette.CompositeView.extend({
        initialize: function () {
            this.collection.startPolling();
        }
        // some code.
    });
});
Lorraine Bernard
  • 13,000
  • 23
  • 82
  • 134
  • What would you like to optimize - fetching only certain elements or rendering only the changed parts? How do you recognize `new elements`? Do you create them on the client or on the server? – Torsten Walter Aug 07 '12 at 09:17
  • For example, I simply wont to check the length of the collection. if it does not change the view should be not re-render. – Lorraine Bernard Aug 07 '12 at 09:32
  • You could do this in [collection.parse()](http://backbonejs.org/#Collection-parse) where you have access to the response as well as to the model. Compare both lengths and return nothing if it didn't change. – Torsten Walter Aug 07 '12 at 11:08
  • Duplicated? http://stackoverflow.com/questions/9640095/backbone-js-collection-upsert – fguillen Aug 07 '12 at 12:04
  • 1
    @fguillen maybe just adding the option `{add:true}` to the fetch call should be enough? – Lorraine Bernard Aug 07 '12 at 12:43
  • @fguillen I looked to your [response](http://stackoverflow.com/questions/9640095/backbone-js-collection-upsert). Just one question. What is the best way to call refreshCollection in my case? should I call it after fetching the collection? – Lorraine Bernard Aug 07 '12 at 13:12
  • @LorraineBernard I was looking into `{add:true}` but I think it doesn't fit with what you want because this _option_ will make the Collection to `add` **all the fetched Models** to the collection. Not looking if the Models are already there, neither looking for removing/updating actual elements. – fguillen Aug 07 '12 at 13:56
  • @LorraineBernard you have to have a helper Collection that will be the one executes the `fetch()` and a persisten Collection that will be the one to be _updated_. I have added an example to my answer, [check it out](http://stackoverflow.com/a/9663670/316700) – fguillen Aug 07 '12 at 14:09

0 Answers0