I have a model:
window.LayerModel = Backbone.Model.extend({
defaults:{
'id':'unknow',
'type':'unkonw',
'preId':'unknow',
'data': {}
}
}
then I defined a collection:
window.LayerManageCollection = Backbone.Collection.extend({
model: LayerModel
})
then I init it in view:
window.LayerMasterManageView = Backbone.View.extend({
initialize: function () {
//here I have a gobal javascript variable named "localdata"
//which have 17 items in an array,each of them is object type
this.collection = new LayerManageCollection(localdata);
console.log('this collection', this.collection);
}
}
but the collection’s length is 1!,and only have one model!,
the strange thing is ,if a change the "defaults" to "default",the collection result is as I desired ,which length is 17
Why this happen? How can I solve this problem?
the default
idAttributefor a model isid. Because you’re defining theidin yourdefaultsyou’re going to always create the same model, since Backbone errors quietly when a duplicate model is created, that’s why you only see 1 model.