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

Commit b7725c7a by Mick Hansen

refactor(instance): change how instance works, make it proactive rather than reactive

1 parent d0c3a16f
...@@ -7,6 +7,7 @@ var Utils = require('./utils') ...@@ -7,6 +7,7 @@ var Utils = require('./utils')
, QueryTypes = require('./query-types') , QueryTypes = require('./query-types')
, Promise = require('./promise') , Promise = require('./promise')
, _ = require('lodash') , _ = require('lodash')
, primitives = ["string", "number", "boolean"]
, defaultsOptions = { raw: true } , defaultsOptions = { raw: true }
, deprecatedSeen = {} , deprecatedSeen = {}
, deprecated = function(message) { , deprecated = function(message) {
...@@ -39,6 +40,7 @@ module.exports = (function() { ...@@ -39,6 +40,7 @@ module.exports = (function() {
var Instance = function(values, options) { var Instance = function(values, options) {
this.dataValues = {}; this.dataValues = {};
this._previousDataValues = {}; this._previousDataValues = {};
this._changed = {};
this.__options = this.Model.options; this.__options = this.Model.options;
this.options = options || {}; this.options = options || {};
this.hasPrimaryKeys = this.Model.options.hasPrimaryKeys; this.hasPrimaryKeys = this.Model.options.hasPrimaryKeys;
...@@ -369,8 +371,9 @@ module.exports = (function() { ...@@ -369,8 +371,9 @@ module.exports = (function() {
} }
} }
if (!options.raw && originalValue !== value) { if (!options.raw && (primitives.indexOf(typeof value) === -1 || value !== originalValue)) {
this._previousDataValues[key] = originalValue; this._previousDataValues[key] = originalValue;
this.changed(key, true);
} }
this.dataValues[key] = value; this.dataValues[key] = value;
} }
...@@ -394,13 +397,15 @@ module.exports = (function() { ...@@ -394,13 +397,15 @@ module.exports = (function() {
* @param {String} [key] * @param {String} [key]
* @return {Boolean|Array} * @return {Boolean|Array}
*/ */
Instance.prototype.changed = function(key) { Instance.prototype.changed = function(key, value) {
if (key) { if (key) {
if (this.Model._isDateAttribute(key) && this._previousDataValues[key] && this.dataValues[key]) { if (value !== undefined) {
return this._previousDataValues[key].valueOf() !== this.dataValues[key].valueOf(); this._changed[key] = value;
return this;
} }
return this._previousDataValues[key] !== this.dataValues[key]; return this._changed[key];
} }
var changed = Object.keys(this.dataValues).filter(function(key) { var changed = Object.keys(this.dataValues).filter(function(key) {
return this.changed(key); return this.changed(key);
}.bind(this)); }.bind(this));
...@@ -674,6 +679,7 @@ module.exports = (function() { ...@@ -674,6 +679,7 @@ module.exports = (function() {
.then(function(result) { .then(function(result) {
options.fields.forEach(function (field) { options.fields.forEach(function (field) {
result._previousDataValues[field] = result.dataValues[field]; result._previousDataValues[field] = result.dataValues[field];
self.changed(field, false);
}); });
self.isNewRecord = false; self.isNewRecord = false;
return result; return result;
......
...@@ -427,21 +427,6 @@ describe(Support.getTestDialectTeaser('DAO'), function() { ...@@ -427,21 +427,6 @@ describe(Support.getTestDialectTeaser('DAO'), function() {
}); });
}); });
it('setting the same value twice should not impact the result', function() {
var User = this.sequelize.define('User', {
name: {type: DataTypes.STRING}
});
var user = User.build({
name: 'Jan Meier'
});
user.set('name', 'Mick Hansen');
user.set('name', 'Mick Hansen');
expect(user.changed('name')).to.be.true;
expect(user.changed()).to.be.ok;
expect(user.isDirty).to.be.true;
expect(user.previous('name')).to.equal('Jan Meier');
});
it('should be available to a afterUpdate hook', function () { it('should be available to a afterUpdate hook', function () {
var User = this.sequelize.define('User', { var User = this.sequelize.define('User', {
name: {type: DataTypes.STRING} name: {type: DataTypes.STRING}
......
'use strict';
/* jshint -W030 */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + '/../../../lib/data-types')
, current = Support.sequelize;
describe(Support.getTestDialectTeaser('Instance'), function() {
describe.only('changed', function () {
beforeEach(function () {
this.User = this.sequelize.define('User', {
name: DataTypes.STRING,
birthdate: DataTypes.DATE,
meta: DataTypes.JSON
});
});
it('should return true for changed primitive', function () {
var user = this.User.build({
name: 'a'
}, {
isNewRecord: false,
raw: true
});
user.set('name', 'b');
expect(user.changed('name')).to.equal(true);
});
it('should return falsy for unchanged primitive', function () {
var user = this.User.build({
name: 'a'
}, {
isNewRecord: false,
raw: true
});
user.set('name', 'a');
expect(user.changed('name')).not.to.be.ok;
});
it('should return true for multiple changed values', function () {
var user = this.User.build({
name: 'a',
birthdate: new Date(new Date() - 10)
}, {
isNewRecord: false,
raw: true
});
user.set('name', 'b');
user.set('birthdate', new Date());
expect(user.changed('name')).to.equal(true);
expect(user.changed('birthdate')).to.equal(true);
});
it('should return true for changed JSON with same object', function () {
var user = this.User.build({
meta: {
city: 'Copenhagen'
}
}, {
isNewRecord: false,
raw: true
});
var meta = user.get('meta');
meta.city = 'Stockholm';
user.set('meta', meta);
expect(user.changed('meta')).to.equal(true);
});
});
});
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!