I do not recommend overriding Model.toJSON(), because perhaps you want to use the JSON representation in other parts of the code, for example, when transferring the Backbone model to a micro-template.
- Model:
sync: function(method, model, options) {
if (method == 'update' || method == 'create') {
var newModel = this.clone();
newModel.unset('ignoredAttribute', {silent: true);
return Backbone.sync.call(newModel, method, newModel, options);
} else {
return Backbone.sync.call(this, method, this, options);
}
},
ignoredAttribute.
:
var CSEvent = Backbone.Model.extend({
idAttribute: "_id",
urlRoot : '/api/events',
defaults: {
title : "",
type : "Native",
repeatOrOneTime : "OneTime",
selected : false
},
sync: function(method, model, options) {
if (method == 'update' || method == 'create') {
var newModel = this.clone();
newModel.unset('selected', {silent: true);
return Backbone.sync.call(newModel, method, newModel, options);
} else {
return Backbone.sync.call(this, method, this, options);
}
}
});
selected ( Backbone), trigger. :
var CSEvent = Backbone.Model.extend({
idAttribute: "_id",
urlRoot : '/api/events',
selected : false,
defaults: {
title : "",
type : "Native",
repeatOrOneTime : "OneTime"
},
select: function() {
this.selected = true;
this.trigger('selected');
},
deselect: function() {
this.selected = false;
this.trigger('deselected');
}
});