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 574877a4
authored
Feb 17, 2013
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixes #204 thanks to @bpartridge
1 parent
fe7703c6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
15 deletions
lib/associations/has-many-double-linked.js
lib/associations/has-many-single-linked.js
lib/associations/has-many.js
lib/associations/has-many-double-linked.js
View file @
574877a
...
...
@@ -51,7 +51,11 @@ module.exports = (function() {
var
chainer
=
new
Utils
.
QueryChainer
,
association
=
self
.
__factory
.
target
.
associations
[
self
.
__factory
.
associationAccessor
]
,
foreignIdentifier
=
association
.
isSelfAssociation
?
association
.
foreignIdentifier
:
association
.
identifier
,
unassociatedObjects
=
newAssociations
.
filter
(
function
(
obj
)
{
return
!
obj
.
equalsOneOf
(
oldAssociations
)
})
,
unassociatedObjects
=
newAssociations
.
filter
(
function
(
obj
)
{
return
!
Utils
.
_
.
find
(
oldAssociations
,
function
(
old
)
{
return
obj
.
id
===
old
.
id
})
})
unassociatedObjects
.
forEach
(
function
(
unassociatedObject
)
{
var
attributes
=
{}
...
...
@@ -69,6 +73,20 @@ module.exports = (function() {
})
}
HasManyDoubleLinked
.
prototype
.
injectAdder
=
function
(
emitterProxy
,
newAssociation
)
{
var
attributes
=
{}
,
association
=
this
.
__factory
.
target
.
associations
[
this
.
__factory
.
associationAccessor
]
,
foreignIdentifier
=
association
.
isSelfAssociation
?
association
.
foreignIdentifier
:
association
.
identifier
;
attributes
[
this
.
__factory
.
identifier
]
=
this
.
instance
.
id
attributes
[
foreignIdentifier
]
=
newAssociation
.
id
this
.
__factory
.
connectorDAO
.
create
(
attributes
)
.
success
(
function
()
{
emitterProxy
.
emit
(
'success'
,
newAssociation
)
})
.
error
(
function
(
err
)
{
emitterProxy
.
emit
(
'error'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitterProxy
.
emit
(
'sql'
,
sql
)
})
}
// private
var
destroyObsoleteAssociations
=
function
(
oldAssociations
,
newAssociations
)
{
...
...
@@ -77,7 +95,12 @@ module.exports = (function() {
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
var
chainer
=
new
Utils
.
QueryChainer
()
var
foreignIdentifier
=
self
.
__factory
.
target
.
associations
[
self
.
__factory
.
associationAccessor
].
identifier
var
obsoleteAssociations
=
oldAssociations
.
filter
(
function
(
obj
)
{
return
!
obj
.
equalsOneOf
(
newAssociations
)
})
var
obsoleteAssociations
=
Utils
.
_
.
filter
(
oldAssociations
,
function
(
old
)
{
// Return only those old associations that are not found in new
return
!
Utils
.
_
.
find
(
newAssociations
,
function
(
obj
)
{
return
obj
.
id
===
old
.
id
})
})
if
(
obsoleteAssociations
.
length
===
0
)
{
return
emitter
.
emit
(
'success'
,
null
)
...
...
lib/associations/has-many-single-linked.js
View file @
574877a
...
...
@@ -19,15 +19,25 @@ module.exports = (function() {
var
self
=
this
,
options
=
this
.
__factory
.
options
,
chainer
=
new
Utils
.
QueryChainer
()
,
obsoleteAssociations
=
oldAssociations
.
filter
(
function
(
old
)
{
return
!
Utils
.
_
.
find
(
newAssociations
,
function
(
obj
)
{
return
obj
.
id
===
old
.
id
})
})
,
unassociatedObjects
=
newAssociations
.
filter
(
function
(
obj
)
{
return
!
Utils
.
_
.
find
(
oldAssociations
,
function
(
old
)
{
return
obj
.
id
===
old
.
id
})
})
// clear the old associations
o
ld
Associations
.
forEach
(
function
(
associatedObject
)
{
o
bsolete
Associations
.
forEach
(
function
(
associatedObject
)
{
associatedObject
[
self
.
__factory
.
identifier
]
=
null
chainer
.
add
(
associatedObject
.
save
())
})
// set the new associations
newAssociation
s
.
forEach
(
function
(
associatedObject
)
{
unassociatedObject
s
.
forEach
(
function
(
associatedObject
)
{
associatedObject
[
self
.
__factory
.
identifier
]
=
self
.
instance
.
id
chainer
.
add
(
associatedObject
.
save
())
})
...
...
@@ -38,5 +48,14 @@ module.exports = (function() {
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
}
HasManySingleLinked
.
prototype
.
injectAdder
=
function
(
emitterProxy
,
newAssociation
)
{
newAssociation
[
this
.
__factory
.
identifier
]
=
this
.
instance
.
id
newAssociation
.
save
()
.
success
(
function
()
{
emitterProxy
.
emit
(
'success'
,
newAssociation
)
})
.
error
(
function
(
err
)
{
emitterProxy
.
emit
(
'error'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitterProxy
.
emit
(
'sql'
,
sql
)
})
}
return
HasManySingleLinked
})()
lib/associations/has-many.js
View file @
574877a
...
...
@@ -151,19 +151,18 @@ module.exports = (function() {
obj
[
this
.
accessors
.
add
]
=
function
(
newAssociatedObject
)
{
var
instance
=
this
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
(
)
{
instance
[
self
.
accessors
.
get
]()
.
error
(
function
(
err
){
customEventE
mitter
.
emit
(
'error'
,
err
)})
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
instance
[
self
.
accessors
.
get
](
{
where
:
{
id
:
newAssociatedObject
.
id
}}
)
.
error
(
function
(
err
){
e
mitter
.
emit
(
'error'
,
err
)})
.
success
(
function
(
currentAssociatedObjects
)
{
if
(
!
newAssociatedObject
.
equalsOneOf
(
currentAssociatedObjects
))
currentAssociatedObjects
.
push
(
newAssociatedObject
)
instance
[
self
.
accessors
.
set
](
currentAssociatedObjects
)
.
success
(
function
(
instances
)
{
customEventEmitter
.
emit
(
'success'
,
instances
)
})
.
error
(
function
(
err
)
{
customEventEmitter
.
emit
(
'error'
,
err
)
})
if
(
currentAssociatedObjects
.
length
===
0
)
{
var
Class
=
self
.
connectorDAO
?
HasManyMultiLinked
:
HasManySingleLinked
new
Class
(
self
,
instance
).
injectAdder
(
emitter
,
newAssociatedObject
)
}
else
{
emitter
.
emit
(
'success'
,
newAssociatedObject
);
}
})
})
return
customEventEmitter
.
run
()
}).
run
()
}
obj
[
this
.
accessors
.
remove
]
=
function
(
oldAssociatedObject
)
{
...
...
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