Reputation: 6938
The following would be the model of a simple backbone application. So consider i want to have more than one url calls, i am told i should be using collections(definition is a collection of models).
Model format :
var Books1 = Backbone.Model.extend({
urlRoot: '/books1'
});
var Books2 = Backbone.Model.extend({
urlRoot: '/books2'
});
Collection format :
var Books = Backbone.Collection.extend({
url: '/books'
});
My question is how can i like combine more than one model in a collection ?
To be more clear :
Consider i have currently 8-10 models in a single view(need to call 8+ server requests from a single view). I am succesfully doing this via models. But from my initial research i came to conclusion that i should be using collection which will be a collection of all the models used. So by passing the collection to view, i will be able to call out different models at different part of view as requried.
And how do i use it in a view?
To be more clear :
for a model now i use this.model.save()
or newmodel.save()
after declaring the model before the place where i need to have the request done.
Cheers
Upvotes: 1
Views: 159
Reputation: 157
lots of properties is handled via http://underscorejs.org/#result so you can pass urlRoot as function
var Books = Backbone.Model.extend({
urlRoot: function(){
return condition ? '/books' : '/bum'
}
});
same in collection. But I don't think so, its a good idea to combine 2 or more models in one collection. Just make a new collection for another model, if its possible....
what about view?
you can pass collection to view instance like this:
var SomeView = Backbone.View.extend({
initialize: function(opt){
this.collection = new opt.collection
}
});
var someView = new SomeView({collection: Books})
Upvotes: 1