My aplication has this structure:
Project (model)
-> tracks (collection)
-> track (model)
-> clips (collection)
clip (model)
I need to fetch only parent project model. It will cause change of all data structure. I get JSON
{ "_id" : "123",
"name" : "name",
"tracks" : [ { "clips" : [ { "audioName" : "audio name",
"audioPath" : "audio/path.wav",
"duration" : 123,
"id" : "track0-1"
} ],
"mute" : false,
"name" : "track0",
"selected" : false,
"volume" : 100
},
{ "clips" : [ ],
"mute" : false,
"name" : "track1",
"selected" : false,
"volume" : 100
}
]
}
I have parse method:
parse: function (data) {
this.get('tracks').reset(data.tracks);
delete data.tracks;
return data;
}
I am not able to parse clips. In model track, attribute clips has behavior like javascript array instead of backbone model.
How can I parse clips?
parse is only used to parse responses from the server. So you won't be able to use it to create your clips collection.
So you may want to change the way you do that (maybe have a look at Backbone-relational, I think it deals with this kind of stuff). Here's a possible solution (to be put in your model):
initialize: function() {
this.listenTo(this, 'change:clips', this.onChangeClips);
// the rest of your stuff
},
onChangeClips: function() {
var clips = this.get('clips');
if(Object.prototype.toString.call(clips) === '[object Array]')
this.set('clips', new Clips(clips), {silent: true});
}
Source to test if array: JavaScript: Check if object is array?
Note: this will remove any reference to an existing collection (which you seem to have), so you may want to keep a reference to your collection in your model (like in a _clips attribute) to reset it with the new clips array.