0

So there's a piece of functionality with which I've been struggling for a while now. I'm using .where() method in order to retrieve an array of objects from the Collection and then I reset this Collection with this array.

    # Fetch the collection
    collection = App.request(collection:entites)
    console.log collection
    >collection {length: 25, models: Array[25] ... }

When the event fires it passes options for .where() method and starts reset process:

    # Get new models
    new_models = collection.where(options)

    # Reset collection with the new models
    collection.on 'reset', (model, options) ->
        console.log options.previousModels
        return

    collection.reset(new_models)
    console.log collection
    >collection {length: 5, models: Array[5] ... }

In the View responsible of rendering this collection I listen to the 'reset' event and render the View accordingly.

    initialize: ->
        @listenTo(@collection, 'reset', @render)

It works just as expected: event fires, collections undergoes reset and the View re-renders reseted collection. But when the event fires second time the collection doesn't sync with the server and new_models = collection.where(options) receives a collection that was already reseted in a previous event run and returns an empty array.

What are my options here? Each event run I need an initial collection of all models to work with. Should I just request new instance of the collection on the each run or can I make it in a more cleaner manner, i.e. save original state somewhere and pass it for the event run instead of fetching new collection from the server? Please advise.

curious_gudleif
  • 572
  • 1
  • 4
  • 19
  • What is the point to reset the collection? You can use custom Events in backbone to trigger functions after you find the filtered set of models from the collection. In this way, your original collection is untouched. And can filter on that with different options. – manasi sakhare Sep 28 '15 at 05:40
  • Well, I'm new to Backbone and JavaScript in general, but as to my understanding I have to pass a valid Backbone Collection to the view. Whilst .where() method returns an Array of Objects, I use reset in order to repopulate collection with these objects, converting them to a set of models. I'm really open to any suggestions and directions, if there's a better way to handle it, please, do tell. @manasisakhare – curious_gudleif Sep 29 '15 at 04:02

1 Answers1

0

Yes. Another way to achieve it is, when you filter the collection, using .where(), you can trigger Backbone.Events custom event which you view can listen to. This way, the original collection does not reset, there only is a change in array which will trigger the custom event.

A sample code for using custom events in backbone is:

var object = {};

_.extend(object, Backbone.Events);

object.on("collectionFiltered", function(arrayOfFilteredModels) {
  // do stuff to render your view with the new set of array.
  // You can use underscore templating to traverse the array for rendering view.
});

object.trigger("collectionFiltered", collection.where(options);
manasi sakhare
  • 1,051
  • 7
  • 18