Setters enabled in new and create

This commit is contained in:
Anatoliy Chakkaev 2012-04-19 19:01:40 +04:00
parent 02138b8965
commit 222b457b3f
2 changed files with 48 additions and 15 deletions

View File

@ -24,10 +24,14 @@ jutil.inherits(AbstractClass, Hookable);
* @param {Object} data - initial object data * @param {Object} data - initial object data
*/ */
function AbstractClass(data) { function AbstractClass(data) {
this._initProperties(data, true);
}
AbstractClass.prototype._initProperties = function (data, applySetters) {
var self = this; var self = this;
var ds = this.constructor.schema.definitions[this.constructor.modelName]; var ctor = this.constructor;
var ds = ctor.schema.definitions[ctor.modelName];
var properties = ds.properties; var properties = ds.properties;
var settings = ds.setings;
data = data || {}; data = data || {};
if (data.id) { if (data.id) {
@ -59,15 +63,15 @@ function AbstractClass(data) {
// Public setters and getters // Public setters and getters
Object.defineProperty(this, attr, { Object.defineProperty(this, attr, {
get: function () { get: function () {
if (this.constructor.getter[attr]) { if (ctor.getter[attr]) {
return this.constructor.getter[attr].call(this); return ctor.getter[attr].call(this);
} else { } else {
return this[_attr]; return this[_attr];
} }
}, },
set: function (value) { set: function (value) {
if (this.constructor.setter[attr]) { if (ctor.setter[attr]) {
this.constructor.setter[attr].call(this, value); ctor.setter[attr].call(this, value);
} else { } else {
this[_attr] = value; this[_attr] = value;
} }
@ -77,10 +81,14 @@ function AbstractClass(data) {
}); });
if (data.hasOwnProperty(attr)) { if (data.hasOwnProperty(attr)) {
if (applySetters && ctor.setter[attr]) {
ctor.setter[attr].call(this, data[attr]);
}
// Getter for initial property // Getter for initial property
Object.defineProperty(this, attr_was, { Object.defineProperty(this, attr_was, {
writable: true, writable: true,
value: data[attr], value: this[_attr],
configurable: true, configurable: true,
enumerable: false enumerable: false
}); });
@ -153,8 +161,7 @@ AbstractClass.create = function (data, callback) {
if (data instanceof AbstractClass && !data.id) { if (data instanceof AbstractClass && !data.id) {
obj = data; obj = data;
data = obj.toObject(true); data = obj.toObject(true);
// recall constructor to update _was property states (maybe bad idea) this.prototype._initProperties.call(obj, data, false);
this.call(obj, data);
create(); create();
} else { } else {
obj = new this(data); obj = new this(data);
@ -257,10 +264,12 @@ AbstractClass.find = function find(id, cb) {
if (cached) { if (cached) {
obj = cached; obj = cached;
substractDirtyAttributes(obj, data); substractDirtyAttributes(obj, data);
this.call(obj, data); // maybe just obj._initProperties(data); instead of
this.prototype._initProperties.call(obj, data);
} else { } else {
data.id = id; data.id = id;
obj = new this(data); obj = new this();
obj._initProperties(data, false);
addToCache(this, id); addToCache(this, id);
} }
} }
@ -303,9 +312,11 @@ AbstractClass.all = function all(params, cb) {
obj = cached; obj = cached;
// keep dirty attributes untouthed (remove from dataset) // keep dirty attributes untouthed (remove from dataset)
substractDirtyAttributes(obj, d); substractDirtyAttributes(obj, d);
constr.call(obj, d); // maybe just obj._initProperties(d);
constr.prototype._initProperties.call(obj, d);
} else { } else {
obj = new constr(d); obj = new constr;
obj._initProperties(d, false);
if (obj.id) addToCache(constr, obj); if (obj.id) addToCache(constr, obj);
} }
return obj; return obj;
@ -433,7 +444,7 @@ AbstractClass.prototype.save = function (options, callback) {
if (err) { if (err) {
console.log(err); console.log(err);
} else { } else {
inst.constructor.call(inst, data); inst._initProperties(data, false);
} }
updateDone.call(inst, function () { updateDone.call(inst, function () {
saveDone.call(inst, function () { saveDone.call(inst, function () {
@ -558,7 +569,7 @@ AbstractClass.prototype.updateAttributes = function updateAttributes(data, cb) {
inst._adapter().updateAttributes(model, inst.id, data, function (err) { inst._adapter().updateAttributes(model, inst.id, data, function (err) {
if (!err) { if (!err) {
inst.constructor.call(inst, data); inst._initProperties(data, false);
/* /*
Object.keys(data).forEach(function (key) { Object.keys(data).forEach(function (key) {
inst[key] = data[key]; inst[key] = data[key];

View File

@ -753,6 +753,28 @@ function testOrm(schema) {
}); });
}); });
it('should work with custom setters and getters', function (test) {
User.setter.passwd = function (pass) {
this._passwd = pass + 'salt';
};
var u = new User({passwd: 'qwerty'});
test.equal(u.passwd, 'qwertysalt');
u.save(function (err, user) {
User.find(user.id, function (err, user) {
test.ok(user !== u);
test.equal(user.passwd, 'qwertysalt');
User.all({where: {id: user.id}}, function (err, users) {
test.ok(users[0] !== user);
test.equal(users[0].passwd, 'qwertysalt');
User.create({passwd: 'asalat'}, function (err, usr) {
test.equal(usr.passwd, 'asalatsalt');
test.done();
});
});
});
});
});
it('all tests done', function (test) { it('all tests done', function (test) {
test.done(); test.done();
process.nextTick(allTestsDone); process.nextTick(allTestsDone);