I’m trying to add a new virtual field to my backbone model whenever a new item is added, the code is simple:
window.DealModel = Backbone.Model.extend({
defaults: {
title: '',
desc: '',
location: '',
terms: '',
price_orignial: 0,
price_discounted: 0
}
});
window.DealCollection = Backbone.Collection.extend({
model: DealModel,
initialize: function (models, options) {
this.bind('add', this.addTitleShort);
},
addTitleShort: function(rdeal){
rdeal.set('title_short', _.str.prune( rdeal.get('title') , 140, '+++'));
}
});
However, I keep getting a _ Cannot use 'in' operator to search for 'id' in title_short_, not sure what the problem is, appreciate the help.
I think that “
set” uses object hash:But if there was no “title_short” property in the created model, I don’t know if you can “set” it. You can just do
Also you can add a function to Model class that will return the result thus eliminating the need to handle model’s “
change” event:and use it:
myModelInstance.title_short();Hope it helped.