Permalink
Browse files

[core, resource] getters/setters API

  • Loading branch information...
1 parent 8da8cef commit 33a3675fbada4861a52d603be55ebf9ad084082c @indutny indutny committed Nov 7, 2011
Showing with 12 additions and 9 deletions.
  1. +5 −5 lib/resourceful/core.js
  2. +7 −4 lib/resourceful/resource.js
View
@@ -183,20 +183,20 @@ resourceful.defineProperty = function (obj, property, schema) {
// Sanitize defaults and per-creation properties
if (schema.sanitize) {
- var val = obj.readProperty(property);
+ var val = obj.readProperty(property, schema.get);
if (val !== undefined) {
- obj.writeProperty(property, schema.sanitize(val));
+ obj.writeProperty(property, schema.sanitize(val), schema.set);
}
}
Object.defineProperty(obj, property, {
get: function () {
- return this.readProperty(property);
+ return this.readProperty(property, schema.get);
},
set: schema.sanitize ? function (val) {
- return this.writeProperty(property, schema.sanitize(val));
+ return this.writeProperty(property, schema.sanitize(val), schema.set);
} : function (val) {
- return this.writeProperty(property, val);
+ return this.writeProperty(property, val, schema.set);
},
enumerable: true
});
@@ -512,12 +512,15 @@ Resource.prototype.saveAttachment = function (name, data, callback) {
Resource.prototype.destroy = function () {};
Resource.prototype.reload = function () {};
-Resource.prototype.readProperty = function (k) {
- return this._properties[k];
+Resource.prototype.readProperty = function (k, getter) {
+ return getter ? getter.call(this, this._properties[k]) : this._properties[k];
};
-Resource.prototype.writeProperty = function (k, val) {
- return this._properties[k] = val;
+Resource.prototype.writeProperty = function (k, val, setter) {
+ return this._properties[k] = setter ?
+ setter.call(this, val)
+ :
+ val;
};

0 comments on commit 33a3675

Please sign in to comment.