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 832fa070
authored
Apr 20, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added query chainer to wait for multiple queries
1 parent
d5377000
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
67 additions
and
14 deletions
lib/sequelize/associations/has-many.js
lib/sequelize/query-chainer.js
test/Model/has-many.js
lib/sequelize/associations/has-many.js
View file @
832fa07
var
Utils
=
require
(
"./../utils"
)
,
DataTypes
=
require
(
'./../data-types'
)
,
QueryChainer
=
require
(
"./../query-chainer"
)
var
HasMany
=
module
.
exports
=
function
(
srcModel
,
targetModel
,
options
)
{
this
.
source
=
srcModel
...
...
@@ -63,12 +64,12 @@ HasMany.prototype.injectGetter = function(obj) {
HasMany
.
prototype
.
injectSetter
=
function
(
obj
)
{
var
self
=
this
obj
[
this
.
accessors
.
set
]
=
function
(
newAssociatedObject
)
{
obj
[
this
.
accessors
.
set
]
=
function
(
newAssociatedObject
s
)
{
var
instance
=
this
// define the returned customEventEmitter, which will emit the success event once everything is done
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
self
[
self
.
accessors
.
get
]().
on
(
'success'
,
function
(
oldAssociatedObjects
)
{
instance
[
self
.
accessors
.
get
]().
on
(
'success'
,
function
(
oldAssociatedObjects
)
{
if
(
self
.
connectorModel
)
{
// destroy the old association objects
oldAssociatedObjects
.
forEach
(
function
(
associatedObject
)
{
...
...
@@ -76,7 +77,7 @@ HasMany.prototype.injectSetter = function(obj) {
})
// create new one
newAssociatedObject
.
forEach
(
function
(
associatedObject
)
{
newAssociatedObject
s
.
forEach
(
function
(
associatedObject
)
{
var
attributes
=
{}
attributes
[
self
.
identifier
]
=
instance
.
id
attributes
[
self
.
foreignIdentifier
]
=
associatedObject
.
id
...
...
@@ -91,11 +92,14 @@ HasMany.prototype.injectSetter = function(obj) {
})
// set the new one
newAssociatedObject
.
forEach
(
function
(
associatedObject
)
{
var
chainer
=
new
QueryChainer
newAssociatedObjects
.
forEach
(
function
(
associatedObject
)
{
associatedObject
[
self
.
identifier
]
=
instance
.
id
associatedObject
.
save
(
)
chainer
.
add
(
associatedObject
.
save
()
)
})
customEventEmitter
.
emit
(
'success'
,
null
)
chainer
.
run
()
.
on
(
'success'
,
function
()
{
customEventEmitter
.
emit
(
'success'
,
null
)
})
.
on
(
'failure'
,
function
()
{
customEventEmitter
.
emit
(
'failure'
,
null
)
})
}
})
})
...
...
lib/sequelize/query-chainer.js
0 → 100644
View file @
832fa07
var
Utils
=
require
(
"./utils"
)
var
QueryChainer
=
module
.
exports
=
function
(
emitters
)
{
var
self
=
this
this
.
finishedEmits
=
0
this
.
emitters
=
[]
this
.
fails
=
[]
this
.
finished
=
false
this
.
runned
=
false
this
.
instance
=
null
emitters
=
emitters
||
[]
emitters
.
forEach
(
function
(
emitter
)
{
self
.
add
(
emitter
)
})
}
Utils
.
addEventEmitter
(
QueryChainer
)
QueryChainer
.
prototype
.
add
=
function
(
emitter
)
{
this
.
observeEmitter
(
emitter
)
this
.
emitters
.
push
(
emitter
)
}
QueryChainer
.
prototype
.
observeEmitter
=
function
(
emitter
)
{
var
self
=
this
emitter
.
on
(
'success'
,
function
(){
self
.
finishedEmits
++
;
self
.
finish
()
})
.
on
(
'failure'
,
function
(){
self
.
finishedEmits
++
;
self
.
fails
.
push
(
emitter
);
self
.
finish
()
})
}
QueryChainer
.
prototype
.
finish
=
function
(
result
)
{
this
.
finished
=
(
this
.
finishedEmits
==
this
.
emitters
.
length
)
if
(
this
.
finished
&&
this
.
runned
)
{
this
.
instance
.
emit
(
this
.
fails
.
length
==
0
?
'success'
:
'failure'
,
result
)
}
}
QueryChainer
.
prototype
.
run
=
function
()
{
var
self
=
this
this
.
instance
=
new
Utils
.
CustomEventEmitter
(
function
()
{
self
.
runned
=
true
self
.
finish
()
})
return
this
.
instance
}
\ No newline at end of file
test/Model/has-many.js
View file @
832fa07
...
...
@@ -130,26 +130,32 @@ module.exports = {
var
t
=
Task
.
build
({
title
:
'asd'
})
assert
.
isDefined
(
t
.
setUsers
)
assert
.
isDefined
(
t
.
getUsers
)
}
/*
,
'it should set and get the correct objects': function(exit) {
},
'it should set and get the correct objects
- monodirectional
'
:
function
(
exit
)
{
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
})
User.has
One(Task, {as: 'Task
'})
User
.
has
Many
(
Task
,
{
as
:
'Tasks
'
})
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
Task
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
User
.
create
({
username
:
'name'
}).
on
(
'success'
,
function
(
user
)
{
Task.create({title: 'snafu'}).on('success', function(task) {
user.setTask(task).on('success', function() {
user.getTask().on('success', function(task2) {
assert.eql(task.title, task2.title)
Task
.
create
({
title
:
'task1'
}).
on
(
'success'
,
function
(
task1
)
{
Task
.
create
({
title
:
'task2'
}).
on
(
'success'
,
function
(
task2
)
{
user
.
setTasks
([
task1
,
task2
]).
on
(
'success'
,
function
()
{
user
.
getTasks
().
on
(
'success'
,
function
(
tasks
)
{
assert
.
eql
(
tasks
.
length
,
2
)
exit
(
function
(){})
})
})
})
})
})
})
})
}
*/
}
}
\ 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