不要怂,就是干,撸起袖子干!

Commit 589357ac by Mick Hansen

[feat] expose .changed() in .afterUpdate hooks, closes #2674

1 parent bf3be422
......@@ -2,6 +2,10 @@
- [BUG] Fixed issue with subquery creating `include.where` and a paranoid main model.#2749/#2769
- UniqueConstraintErrors will now extend from ValidationError making it possible to catch both with `.catch(ValidationError)`
- [FEATURE] Adds `{save: false}` for belongsTo relationship setters. `user.setOrganization(organization, {save: false})` will then only set the foreign key value, but not trigger a save on `user`.
- [FEATURE] When updating an instance `_previousDataValues` will now be updated after `afterUpdate` hooks have been run rather than before allowing you to use `changed` in `afterUpdate`
#### Backwards compatability changes
- When updating an instance `_previousDataValues` will now be updated after `afterUpdate` hooks have been run rather than before allowing you to use `changed` in `afterUpdate`
# 2.0.0-rc4
- [INTERNALS] Update `inflection` dependency to v1.5.3
......
......@@ -615,11 +615,7 @@ module.exports = (function() {
});
values = _.extend(values, result.dataValues);
// Ensure new values are on Instance, and reset previousDataValues
result.dataValues = _.extend(result.dataValues, values);
options.fields.forEach(function (field) {
result._previousDataValues[field] = result.dataValues[field];
});
})
.tap(function(result) {
// Run after hook
......@@ -628,6 +624,9 @@ module.exports = (function() {
}
})
.then(function(result) {
options.fields.forEach(function (field) {
result._previousDataValues[field] = result.dataValues[field];
});
return result;
});
});
......
......@@ -426,6 +426,34 @@ describe(Support.getTestDialectTeaser('DAO'), function() {
expect(user.isDirty).to.be.true;
expect(user.previous('name')).to.equal('Jan Meier');
});
it('should be available to a afterUpdate hook', function () {
var User = this.sequelize.define('User', {
name: {type: DataTypes.STRING}
});
var changed;
User.afterUpdate(function (instance) {
changed = instance.changed();
return;
});
return User.sync({force: true}).then(function () {
return User.create({
name: 'Ford Prefect'
});
}).then(function (user) {
return user.update({
name: 'Arthur Dent'
});
}).then(function (user) {
expect(changed).to.be.ok;
expect(changed.length).to.be.ok;
expect(changed.indexOf('name') > -1).to.be.ok;
expect(user.changed()).not.to.be.ok;
});
});
});
describe('previous', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!