Reputation: 3334
Is there a way to built more complex Model in Backbone.js, let me explain by an example :
This is a Json Session object :
{
id: "17",
notes: "",
start: "2012-10-18T15:57:41.511Z",
end: "2012-10-18T19:22:31.875Z",
sessionType: {
id: "1",
name: "Life Style",
}
}
When retrieving a Session Object from the server, I would like to have a SessionType Backbone.Model in order to add some business logic around this object.
So far I'm only able to retrieve an Object Session with a dummy SessionType, I can't add any logic on it because It doesn't belong to any Backbone model.
Upvotes: 3
Views: 1054
Reputation: 4244
@Amulya is 100% correct. However, if you want the Session Model without having to call getSessionType(), I would look at using the the built in parse method and creating your model from there.
If your Session model is related to your model, I would look at using Backbone Relational. Since Backbone does not handle relationships, the plugin listed above does a fine job in filling the gap without too much manual labour.
Upvotes: 2
Reputation: 7698
You can try this:
window.SessionType = Backbone.Model.extend({
initialize:function () {
},
});
Then in your session model, have a method:
window.Session = Backbone.Model.extend({
initialize:function () {
},
getSessionType () {
return new SessionType(this.get('sessionType'));
}
});
Now you can call the getSessionType()
method which returns a model that can have your logic.
Upvotes: 3