Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
-3
rated 0 times [  0] [ 3]  / answers: 1 / hits: 56457  / 13 Years ago, thu, june 30, 2011, 12:00:00

I've got the following JSON provided from a server. With this, I want to create a model with a nested model. I am unsure of which is the way to achieve this.



//json
[{
name : example,
layout : {
x : 100,
y : 100,
}
}]


I want these to be converted to two nested backbone models with the following structure:



// structure
Image
Layout
...


So I define the Layout model like so:



var Layout = Backbone.Model.extend({});


But which of the two (if any) techniques below should I use to define the Image model? A or B below?



A



var Image = Backbone.Model.extend({
initialize: function() {
this.set({ 'layout' : new Layout(this.get('layout')) })
}
});


or, B



var Image = Backbone.Model.extend({
initialize: function() {
this.layout = new Layout( this.get('layout') );
}
});

More From » backbone.js

 Answers
14

I have the very same issue while I'm writing my Backbone application. Having to deal with embedded/nested models. I did some tweaks that I thought was a quite elegant solution.



Yes, you can modify the parse method to change a attributes around in the object, but all of that is actually pretty unmaintainable code IMO, and feels more of a hack than a solution.



Here's what I suggest for your example:



First define your Layout Model like so.



var layoutModel = Backbone.Model.extend({});


Then here's your image Model:



var imageModel = Backbone.Model.extend({

model: {
layout: layoutModel,
},

parse: function(response){
for(var key in this.model)
{
var embeddedClass = this.model[key];
var embeddedData = response[key];
response[key] = new embeddedClass(embeddedData, {parse:true});
}
return response;
}
});


Notice that I have not tampered with the model itself, but merely pass back the desired object from the parse method.



This should ensure the structure of the nested model when you're reading from the server. Now, you would notice that saving or setting is actually not handled here because I feel that it makes sense for you to set the nested model explicitly using the proper model.



Like so:



image.set({layout : new Layout({x: 100, y: 100})})


Also take note that you are actually invoking the parse method in your nested model by calling:



new embeddedClass(embeddedData, {parse:true});


You can define as many nested models in the model field as you need.



Of course, if you want to go as far as saving the nested model in its own table. This wouldn't be sufficient. But in the case of reading and saving the object as a whole, this solution should suffice.


[#91421] Tuesday, June 28, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tonisandyp

Total Points: 694
Total Questions: 97
Total Answers: 77

Location: British Indian Ocean Territory
Member since Tue, Feb 22, 2022
2 Years ago
;