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