I'm learning Backbone.
I want to create a list that can contain different models, with different attributes.
For example, listing folder contents, which could include models of type file and models of type folder, in any order.
file : {
title : "",
date : "",
type : "",
yaddayadda : ""
}
folder : {
title : "",
date : "",
haminahamina : ""
}
What is the proper way to represent this in Backbone? Is it possible to have a single Collection with multiple models?
Create a base model that your other models inherit from:
And make the
model
type of the collection be that same base model:You could also do it the backbone way. Check out the docs backbone collection
Basically you would create different models adding a tie breaker attribute say "type" in this case.
Backbone documention is not complete in this case. It will not work when used with
merge:true
option andidAttribute
. In that case you need to: