A custom getters and setters plugin for Backbone.js.
Include Backbone (including underscore.js) in your page before including the Backbone.getters.setters plugin and you're all set to go.
The plugin is tested with Backbone version 0.9.1
Your model should extend Backbone.GSModel instead of Backbone.Model in order to support getters:
var MyModel = Backbone.GSModel.extend({
});
Configure your getters by adding a getter function for each attribute:
var MyModel = Backbone.GSModel.extend({
getters: {
fullName: function() {
return this.get('firstName') + ' ' + this.get('lastName');
}
},
defaults: {
firstName: 'Lady',
lastName: 'Gaga'
}
});
Then simply call the regular get method:
var someModel = new MyModel();
alert(someModel.get('fullName'));
The output of the above will be an alert with the text: 'Lady Gaga'.
Your model should extend Backbone.GSModel instead of Backbone.Model in order to support setters:
var MyModel = Backbone.GSModel.extend({
});
Configure your setters by adding a setter function for each attribute:
var MyModel = Backbone.GSModel.extend({
setters: {
firstName: function(value) {
return value.toUpperCase();
},
lastName:: function(value) {
return value.toLowerCase();
}
},
defaults: {
firstName: 'Lady',
lastName: 'Gaga'
}
});
In the above example, the setters were already called by the defaults hash.
The value of 'firstName' is now 'LADY', the value of 'lastName' is now 'gaga'.
You can also call the set method as usual:
someModel.set('firstName', 'letters');
And now the value of 'firstName' is 'LETTERS'.
You can also set multiple attributes as regular:
someModel.set({
'firstName': 'everything',
'lastName': 'NUMBERS'
});
And now the value of 'firstName' is 'EVERYTHING' and the value of 'lastName' is 'numbers'.