Update attribute + hooks

This commit is contained in:
Anatoliy Chakkaev 2011-11-20 15:47:49 +07:00
parent 56b7a3a3ea
commit e4e4a43175
1 changed files with 26 additions and 16 deletions

View File

@ -323,6 +323,7 @@ AbstractClass.prototype.updateAttribute = function (name, value, cb) {
}; };
AbstractClass.prototype.updateAttributes = function updateAttributes(data, cb) { AbstractClass.prototype.updateAttributes = function updateAttributes(data, cb) {
var inst = this;
var model = this.constructor.modelName; var model = this.constructor.modelName;
Object.keys(data).forEach(function (key) { Object.keys(data).forEach(function (key) {
this[key] = data[key]; this[key] = data[key];
@ -339,23 +340,32 @@ AbstractClass.prototype.updateAttributes = function updateAttributes(data, cb) {
}.bind(this)); }.bind(this));
function update() { function update() {
this.trigger('update', function (done) { this.trigger('save', function (saveDone) {
this._adapter().updateAttributes(model, this.id, data, function (err) { this.trigger('update', function (done) {
if (!err) {
Object.keys(data).forEach(function (key) { Object.keys(data).forEach(function (key) {
this[key] = data[key]; data[key] = this[key];
Object.defineProperty(this, key + '_was', { }.bind(this));
writable: false,
configurable: true, this._adapter().updateAttributes(model, this.id, data, function (err) {
enumerable: false, if (!err) {
value: data[key] Object.keys(data).forEach(function (key) {
inst[key] = data[key];
Object.defineProperty(inst, key + '_was', {
writable: false,
configurable: true,
enumerable: false,
value: data[key]
});
}); });
}.bind(this)); }
} done.call(inst, function () {
done.call(this, function () { saveDone.call(inst, function () {
cb(err); cb(err);
}); });
}.bind(this)); });
}.bind(this));
});
}); });
} }
}; };