Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 87d23614
authored
Jan 02, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
basic changed() functionality
1 parent
6d503d2c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
69 additions
and
46 deletions
lib/dao.js
test/dao/values.test.js
lib/dao.js
View file @
87d2361
...
...
@@ -8,6 +8,7 @@ var Utils = require("./utils")
module
.
exports
=
(
function
()
{
var
DAO
=
function
(
values
,
options
)
{
this
.
dataValues
=
{}
this
.
_previousDataValues
=
{}
this
.
__options
=
this
.
__factory
.
options
this
.
options
=
options
this
.
hasPrimaryKeys
=
this
.
__factory
.
options
.
hasPrimaryKeys
...
...
@@ -46,6 +47,12 @@ module.exports = (function() {
}
})
Object
.
defineProperty
(
DAO
.
prototype
,
'isDirty'
,
{
get
:
function
()
{
return
this
.
changed
()
}
})
Object
.
defineProperty
(
DAO
.
prototype
,
'primaryKeyValues'
,
{
get
:
function
()
{
var
result
=
{}
...
...
@@ -153,22 +160,24 @@ module.exports = (function() {
value
=
!!
value
}
this
.
_previousDataValues
[
key
]
=
originalValue
this
.
dataValues
[
key
]
=
value
}
}
}
}
if
(
Utils
.
hasChanged
(
originalValue
,
value
))
{
//Only dirty the object if the change is not due to id, touchedAt, createdAt or updatedAt being initiated
var
updatedAtAttr
=
Utils
.
_
.
underscoredIf
(
this
.
__options
.
updatedAt
,
this
.
__options
.
underscored
)
,
createdAtAttr
=
Utils
.
_
.
underscoredIf
(
this
.
__options
.
createdAt
,
this
.
__options
.
underscored
)
,
touchedAtAttr
=
Utils
.
_
.
underscoredIf
(
this
.
__options
.
touchedAt
,
this
.
__options
.
underscored
)
if
(
this
.
dataValues
[
key
]
&&
(
key
!=
'id'
&&
key
!=
touchedAtAttr
&&
key
!=
createdAtAttr
&&
key
!=
updatedAtAttr
))
{
this
.
isDirty
=
true
}
DAO
.
prototype
.
changed
=
function
(
key
)
{
if
(
key
)
{
if
(
this
.
_previousDataValues
[
key
]
!==
undefined
)
{
return
this
.
_previousDataValues
[
key
]
!==
this
.
dataValues
[
key
]
}
return
false
}
};
return
Object
.
keys
(
this
.
dataValues
).
some
(
function
(
key
)
{
return
this
.
changed
(
key
)
}.
bind
(
this
))
}
DAO
.
prototype
.
_setInclude
=
function
(
key
,
value
,
options
)
{
if
(
!
Array
.
isArray
(
value
))
value
=
[
value
]
...
...
@@ -437,42 +446,6 @@ module.exports = (function() {
DAO
.
prototype
.
setAttributes
=
function
(
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
)
{
...
...
test/dao/values.test.js
View file @
87d2361
...
...
@@ -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
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment