documentcloud/backbone — Issue #1043: Custom getters/setters for Backbone.Model
Author: berzniz
Created: 2012-02-24 13:46:08+00:00
State: open

As people requested before me, having the ability to easily define getters and setters for attributes will be very handy.

The problem with creating our own setter such as setTitle() is that functions like fetch() will not call it.

I created a plugin (https://github.com/berzniz/backbone.getters.setters) that allows adding getters/setters selectively in a declarative way:

```javascript
var MyModel = Backbone.Model.extend({
getters: {
fullName: function() {
return this.get('firstName') + ' ' + this.get('lastName');
}
},
setters: {
firstName: function(value) {
return value.toUpperCase();
}
}
});
```

Calls to get()/set() are now routed to the getters/setters functions if available.

Will this kind of solution fit in backbone without a need for a plugin?