Created
July 26, 2012 12:34
-
-
Save skrat/3181775 to your computer and use it in GitHub Desktop.
Backbone.Model getters/setter for default attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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