Skip to content

Instantly share code, notes, and snippets.

@skrat
Created July 26, 2012 12:34
Show Gist options
  • Save skrat/3181775 to your computer and use it in GitHub Desktop.
Save skrat/3181775 to your computer and use it in GitHub Desktop.
Backbone.Model getters/setter for default attributes
/**
* Defines getter/setter properties using Backbone.Model defaults
* @param {Function} ctor constructor of Bakcbone.Model
*/
function defaultProperties(ctor) {
if (!ctor.prototype.defaults)
return;
var defattr = function(name) {
Object.defineProperty(ctor.prototype, name, {
get: function() {
return this.get(name);
},
set: function(val) {
var diff = {};
diff[name] = val;
return this.set(diff);
}
});
};
for (var attr in ctor.prototype.defaults) {
if (ctor.prototype.defaults.hasOwnProperty(attr))
defattr(attr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment