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,24 +340,33 @@ AbstractClass.prototype.updateAttributes = function updateAttributes(data, cb) {
}.bind(this)); }.bind(this));
function update() { function update() {
this.trigger('save', function (saveDone) {
this.trigger('update', function (done) { this.trigger('update', function (done) {
Object.keys(data).forEach(function (key) {
data[key] = this[key];
}.bind(this));
this._adapter().updateAttributes(model, this.id, data, function (err) { this._adapter().updateAttributes(model, this.id, data, function (err) {
if (!err) { if (!err) {
Object.keys(data).forEach(function (key) { Object.keys(data).forEach(function (key) {
this[key] = data[key]; inst[key] = data[key];
Object.defineProperty(this, key + '_was', { Object.defineProperty(inst, key + '_was', {
writable: false, writable: false,
configurable: true, configurable: true,
enumerable: false, enumerable: false,
value: data[key] value: data[key]
}); });
}.bind(this)); });
} }
done.call(this, function () { done.call(inst, function () {
saveDone.call(inst, function () {
cb(err); cb(err);
}); });
});
}.bind(this)); }.bind(this));
}); });
});
} }
}; };