afeld/backbone-nested

Question about how model.set handles arrays

Closed this issue · 0 comments

I am having an issue, and wanted some clarification on what could be going on.

I have a backbone collection, which represents a list of images. On first fetching the collection, the server sends the first model back with a high rez image and a thumbnail sized image. Each subsequent item in collection has just the thumbnail. As a user selects an image, if the model has no high res image in the model, I have that model make a fetch on its own to retrieve the full model data from the server, with the high res image.

Currently the server returns the result as an array with a single object in it containing the new model attributes. Not ideal, and we are going to change that, but it makes backbone.nested act strange. When .set is called, the KEY argument is the array result, and VALUE is an object which contains fetch()'s XHR request options. Having the array for the key makes the method set a value for attrPath (line 57 in the code). The consequence of this is that _setAttr is called with the VALUE option being passed in, and these XHR options get set on the model.

Is this by design? Is there a reason why we would want to save this XHR object on the model as an attribute? Or (as I assume is the case) am I doing something wildly wrong in how I am operating?