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

Commit 87d23614 by Mick Hansen

basic changed() functionality

1 parent 6d503d2c
Showing with 69 additions and 46 deletions
...@@ -8,6 +8,7 @@ var Utils = require("./utils") ...@@ -8,6 +8,7 @@ var Utils = require("./utils")
module.exports = (function() { module.exports = (function() {
var DAO = function(values, options) { var DAO = function(values, options) {
this.dataValues = {} this.dataValues = {}
this._previousDataValues = {}
this.__options = this.__factory.options this.__options = this.__factory.options
this.options = options this.options = options
this.hasPrimaryKeys = this.__factory.options.hasPrimaryKeys this.hasPrimaryKeys = this.__factory.options.hasPrimaryKeys
...@@ -46,6 +47,12 @@ module.exports = (function() { ...@@ -46,6 +47,12 @@ module.exports = (function() {
} }
}) })
Object.defineProperty(DAO.prototype, 'isDirty', {
get: function() {
return this.changed()
}
})
Object.defineProperty(DAO.prototype, 'primaryKeyValues', { Object.defineProperty(DAO.prototype, 'primaryKeyValues', {
get: function() { get: function() {
var result = {} var result = {}
...@@ -153,22 +160,24 @@ module.exports = (function() { ...@@ -153,22 +160,24 @@ module.exports = (function() {
value = !!value value = !!value
} }
this._previousDataValues[key] = originalValue
this.dataValues[key] = value this.dataValues[key] = value
} }
} }
}
}
if (Utils.hasChanged(originalValue, value)) { DAO.prototype.changed = function(key) {
//Only dirty the object if the change is not due to id, touchedAt, createdAt or updatedAt being initiated if (key) {
var updatedAtAttr = Utils._.underscoredIf(this.__options.updatedAt, this.__options.underscored) if (this._previousDataValues[key] !== undefined) {
, createdAtAttr = Utils._.underscoredIf(this.__options.createdAt, this.__options.underscored) return this._previousDataValues[key] !== this.dataValues[key]
, touchedAtAttr = Utils._.underscoredIf(this.__options.touchedAt, this.__options.underscored)
if (this.dataValues[key] && (key != 'id' && key != touchedAtAttr && key != createdAtAttr && key != updatedAtAttr)) {
this.isDirty = true
}
} }
return false
} }
}; return Object.keys(this.dataValues).some(function (key) {
return this.changed(key)
}.bind(this))
}
DAO.prototype._setInclude = function(key, value, options) { DAO.prototype._setInclude = function(key, value, options) {
if (!Array.isArray(value)) value = [value] if (!Array.isArray(value)) value = [value]
...@@ -437,42 +446,6 @@ module.exports = (function() { ...@@ -437,42 +446,6 @@ module.exports = (function() {
DAO.prototype.setAttributes = function(updates) { DAO.prototype.setAttributes = function(updates) {
this.set(updates) this.set(updates)
/*var self = this
var readOnlyAttributes = Object.keys(this.__factory.primaryKeys)
readOnlyAttributes.push('id')
if (this.isNewRecord !== true) {
readOnlyAttributes.push(Utils._.underscoredIf(this.daoFactory.options.createdAt, this.daoFactory.options.underscored))
}
// readOnlyAttributes.push(this.daoFactory.options.underscored === true ? 'updated_at' : 'updatedAt')
readOnlyAttributes.push(this.daoFactory.options.deletedAt, this.daoFactory.options.underscored)
var isDirty = this.isDirty
Utils._.each(updates, function(value, attr) {
var updateAllowed = (
(readOnlyAttributes.indexOf(attr) === -1) &&
(readOnlyAttributes.indexOf(Utils._.underscored(attr)) === -1) &&
(self.attributes.indexOf(attr) > -1)
)
if (updateAllowed) {
if (Utils.hasChanged(self[attr], value)) {
isDirty = true
}
self[attr] = value
}
})
// since we're updating the record, we should be updating the updatedAt column..
if (this.daoFactory.options.timestamps === true) {
isDirty = true
self[Utils._.underscoredIf(this.daoFactory.options.updatedAt, this.daoFactory.options.underscored)] = new Date()
}
this.isDirty = isDirty*/
} }
DAO.prototype.destroy = function(options) { DAO.prototype.destroy = function(options) {
......
...@@ -135,5 +135,54 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -135,5 +135,54 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
}) })
}) })
}) })
describe('changed', function () {
it('should return false if previous value is undefined', function () {
var User = this.sequelize.define('User', {
name: {type: DataTypes.STRING}
})
var user = User.build()
user.set('name', 'Mick Hansen')
expect(user.changed('name')).to.be.false
expect(user.changed()).to.be.false
expect(user.isDirty).to.be.false
})
it('should return true if previous value is defined and different', function () {
var User = this.sequelize.define('User', {
name: {type: DataTypes.STRING}
})
var user = User.build({
name: 'Jan Meier'
})
user.set('name', 'Mick Hansen')
expect(user.changed('name')).to.be.true
expect(user.changed()).to.be.true
expect(user.isDirty).to.be.true
})
it('should return false immediately after saving', function (done) {
var User = this.sequelize.define('User', {
name: {type: DataTypes.STRING}
})
var user = User.build({
name: 'Jan Meier'
})
user.set('name', 'Mick Hansen')
expect(user.changed('name')).to.be.true
expect(user.changed()).to.be.true
expect(user.isDirty).to.be.true
user.save().done(function (err) {
expect(user.changed('name')).to.be.false
expect(user.changed()).to.be.false
expect(user.isDirty).to.be.false
done()
})
})
})
}) })
}) })
\ 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!