1

I am trying to reset a backbone collection with an array of models. It gets reset but the model structure is changed (nested one level).

Here is a detailed explanation:

Model

var SeatModel = Backbone.Model.extend({

   defaults:{

  },   
   initialize:function () {
       console.log('Model initialized');
   }

});

Collection

var myCollection = Backbone.Collection.extend({

   url:"",

   parse:function (data) {
   },

   initialize:function () {
       console.log('Collection initialized');
   } 

});

Now, I am executing some logic in a web worker, which generates an array of models. The size of the array varies depending on the url I hit.

When the array is ready, I reset the data in the collection using something like: (Before this, I have instantiated the collection and set it in an service object)

worker.onmessage = function(e) {

                        newDataForCollection = e.data;
                        //update the collection
                        service.get("myCollection").reset(newDataForCollection);
                      };

After getting reset, the structure of the collection gets changed to something like:

models: Array[3154]
   [0...99]
       0:g.Model
            attributes:
                 attributes:
                       price: "12"  

Whereas it should be like:

 models: Array[3154]
       [0...99]
           0:g.Model
                attributes:  
                           price: "12"

Also the number of models in the array gets reduced. (Should have been around 6100 in this case).

I am unable to figure out, what causes the internal structure to get nested by one level on invoking reset on the collection.

Updated Post

Figured it out. We cannot send objects with functions in post message, so the models in the array just have the attributes and no functions. Related Passing objects to a web worker

Community
  • 1
  • 1
Himanshu Arora
  • 2,368
  • 3
  • 22
  • 33
  • How **`newDataForCollection`** is look like? – Paul T. Rawkeen Jul 11 '14 at 10:22
  • @PaulT.Rawkeen it is an array of models, which i am returning from a web worker. – Himanshu Arora Jul 11 '14 at 10:38
  • I've just tried `var coll = new Backbone.Collection();` + `coll.reset([{ id : 1, name : "a" }, { id : 2, name : "b" }, { id : 3, name : "c" }]);` and everything is like supposed to be. Check or present your _true_ **`newDataForCollection`** just to be sure that we are talking the same things. – Paul T. Rawkeen Jul 11 '14 at 10:43
  • @PaulT.Rawkeen Yes, it would work in this case. What I was doing is, passing the array from a web worker. But just got to know that we cannot pass objects with functions from web workers. So, the models no longer remain backbone models once they are received in the main thread. Anyways, thanks for your quick response – Himanshu Arora Jul 11 '14 at 10:51
  • You are welcome! Glad you found the problem. – Paul T. Rawkeen Jul 11 '14 at 10:55

1 Answers1

0

Figured it out. We cannot send objects with functions in post message, so the models in the array just have the attributes and no functions. This was related to issue Passing objects to a web worker

Community
  • 1
  • 1
Himanshu Arora
  • 2,368
  • 3
  • 22
  • 33