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 65e36f7b
authored
Nov 22, 2013
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
first attempt at refactoring getters/setters to use defineproperty on the prototype
1 parent
53007f22
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
49 deletions
lib/dao-factory.js
lib/dao.js
lib/dao-factory.js
View file @
65e36f7
...
@@ -141,26 +141,49 @@ module.exports = (function() {
...
@@ -141,26 +141,49 @@ module.exports = (function() {
})
})
}
}
Utils
.
_
.
each
([
'Get'
,
'Set'
],
function
(
type
)
{
var
attributeManipulation
=
{};
var
prop
=
type
.
toLowerCase
()
,
opt
=
prop
+
'terMethods'
Utils
.
_
.
each
([
'get'
,
'set'
],
function
(
type
)
{
,
meth
=
'__define'
+
type
+
'ter__
'
var
opt
=
type
+
'terMethods
'
,
funcs
=
Utils
.
_
.
isObject
(
self
.
options
[
opt
])
?
self
.
options
[
opt
]
:
{}
,
funcs
=
Utils
.
_
.
isObject
(
self
.
options
[
opt
])
?
self
.
options
[
opt
]
:
{}
Utils
.
_
.
each
(
self
.
rawAttributes
,
function
(
attr
,
name
)
{
if
(
attr
.
hasOwnProperty
(
prop
))
{
Utils
.
_
.
each
(
self
.
rawAttributes
,
function
(
attribute
,
name
)
{
funcs
[
name
]
=
attr
[
prop
]
if
(
attribute
.
hasOwnProperty
(
type
))
{
funcs
[
name
]
=
attribute
[
type
]
}
else
if
(
typeof
funcs
[
name
]
===
"undefined"
)
{
if
(
type
===
'get'
)
{
funcs
[
name
]
=
function
()
{
return
this
.
dataValues
[
attribute
];
}
}
if
(
type
===
'set'
)
{
funcs
[
name
]
=
function
(
value
)
{
if
(
Utils
.
hasChanged
(
this
.
dataValues
[
attribute
],
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
[
attribute
]
||
(
attribute
!=
'id'
&&
attribute
!=
touchedAtAttr
&&
attribute
!=
createdAtAttr
&&
attribute
!=
updatedAtAttr
))
{
this
.
isDirty
=
true
}
}
this
.
dataValues
[
attribute
]
=
value
}
}
}
}
})
})
Utils
.
_
.
each
(
funcs
,
function
(
fct
,
name
)
{
Utils
.
_
.
each
(
funcs
,
function
(
fct
,
name
)
{
if
(
!
Utils
.
_
.
isFunction
(
fct
))
{
if
(
!
attributeManipulation
[
name
])
attributeManipulation
[
name
]
=
{}
throw
new
Error
(
type
+
'ter for "'
+
name
+
'" is not a function.'
)
attributeManipulation
[
name
][
type
]
=
fct
}
self
.
DAO
.
prototype
[
meth
](
name
,
fct
)
})
})
})
})
for
(
var
attribute
in
attributeManipulation
)
{
if
(
attributeManipulation
.
hasOwnProperty
(
attribute
))
{
Object
.
defineProperty
(
this
.
DAO
.
prototype
,
attribute
,
attributeManipulation
[
attribute
])
}
}
this
.
DAO
.
prototype
.
attributes
=
Object
.
keys
(
this
.
DAO
.
prototype
.
rawAttributes
);
this
.
DAO
.
prototype
.
attributes
=
Object
.
keys
(
this
.
DAO
.
prototype
.
rawAttributes
);
this
.
DAO
.
prototype
.
booleanValues
=
[]
this
.
DAO
.
prototype
.
booleanValues
=
[]
...
...
lib/dao.js
View file @
65e36f7
...
@@ -427,43 +427,6 @@ module.exports = (function() {
...
@@ -427,43 +427,6 @@ module.exports = (function() {
value
=
!!
value
value
=
!!
value
}
}
var
has
=
(
function
(
o
)
{
var
predef
=
Object
.
getOwnPropertyDescriptor
(
o
,
attribute
);
if
(
predef
&&
predef
.
hasOwnProperty
(
'value'
))
{
return
true
// true here means 'this property exist as a simple value property, do not place setters or getters at all'
}
return
{
get
:
(
predef
&&
predef
.
hasOwnProperty
(
'get'
)
?
predef
.
get
:
null
)
||
o
.
__lookupGetter__
(
attribute
),
set
:
(
predef
&&
predef
.
hasOwnProperty
(
'set'
)
?
predef
.
set
:
null
)
||
o
.
__lookupSetter__
(
attribute
)
};
})(
this
);
// @ node-v0.8.19:
// calling __defineGetter__ destroys any previously defined setters for the attribute in
// question *if* that property setter was defined on the object's prototype (which is what
// we do in dao-factory) ... therefore we need to [re]define both the setter and getter
// here with either the function that already existed OR the default/automatic definition
//
// (the same is true for __defineSetter and 'prototype' getters)
if
(
has
!==
true
)
{
this
.
__defineGetter__
(
attribute
,
has
.
get
||
function
()
{
return
this
.
dataValues
[
attribute
];
});
this
.
__defineSetter__
(
attribute
,
has
.
set
||
function
(
v
)
{
if
(
Utils
.
hasChanged
(
this
.
dataValues
[
attribute
],
v
))
{
//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
[
attribute
]
||
(
attribute
!=
'id'
&&
attribute
!=
touchedAtAttr
&&
attribute
!=
createdAtAttr
&&
attribute
!=
updatedAtAttr
))
{
this
.
isDirty
=
true
}
}
this
.
dataValues
[
attribute
]
=
v
});
}
this
[
attribute
]
=
value
;
this
[
attribute
]
=
value
;
}
}
...
...
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