I want to increment the model position attribute everytime a new model is added to the collection, I've tried converting defaults to be a function that returns a position equalled to ++collection with no success. Can anyone advise the best approach to do this?
var Col = Backbone.Collection.extend()
var Mod = Backbone.Model.extend({
defaults() {
return {
position: ++this.collection.length
}
}
})
var col = new Col([{
id: 1
}, {
id: 2
}])
col.toJSON() // returns [{id: 1}, {id: 2}]