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

Commit b8e0f378 by Jan Aagaard Meier

Merge pull request #4655 from seegno-forks/enhancement/improve-instance-previous

Improve `previous()` instance method
2 parents 5130ba63 30045647
# NEXT
- [FIXED] Mark unscoped model as `.scoped`, to prevent injection of default scope on includes [#4663](https://github.com/sequelize/sequelize/issues/4663)
- [ADDED] `.previous` now returns and object of previous values when called without `key`. This brings the API in line with `.changed`
# 3.12.1
- [FIXED] Mark postgres connection as invalid if the connection is reset [#4661](https://github.com/sequelize/sequelize/pull/4661)
......
......@@ -416,11 +416,20 @@ Instance.prototype.changed = function(key, value) {
/**
* Returns the previous value for key from `_previousDataValues`.
* @param {String} key
* @return {any}
*
* If called without a key, returns the previous values for all values which have changed
*
* @param {String} [key]
* @return {any|Array<any>}
*/
Instance.prototype.previous = function(key) {
return this._previousDataValues[key];
if (key) {
return this._previousDataValues[key];
}
return _.pick(this._previousDataValues, function(value, key) {
return this.changed(key);
}, this);
};
Instance.prototype._setInclude = function(key, value, options) {
......
......@@ -450,6 +450,23 @@ describe(Support.getTestDialectTeaser('DAO'), function() {
});
describe('previous', function() {
it('should return an object with the previous values', function() {
var User = this.sequelize.define('User', {
name: { type: DataTypes.STRING },
title: { type: DataTypes.STRING }
});
var user = User.build({
name: 'Jan Meier',
title: 'Mr'
});
user.set('name', 'Mick Hansen');
user.set('title', 'Dr');
expect(user.previous()).to.eql({ name: 'Jan Meier', title: 'Mr' });
});
it('should return the previous value', function() {
var User = this.sequelize.define('User', {
name: {type: DataTypes.STRING}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!