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 7f53f984
authored
Apr 18, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
correctly disconnecting associated objects from the source when associating with a new one
1 parent
fea413f1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
59 additions
and
11 deletions
lib/sequelize/associations/has-one.js
lib/sequelize/utils.js
test/Model/has-one.js
lib/sequelize/associations/has-one.js
View file @
7f53f98
...
...
@@ -5,6 +5,10 @@ var HasOne = module.exports = function(srcModel, targetModel, options) {
this
.
source
=
srcModel
this
.
target
=
targetModel
this
.
options
=
options
this
.
accessors
=
{
get
:
Utils
.
_
.
camelize
(
'get_'
+
(
this
.
options
.
as
||
this
.
target
.
tableName
)),
set
:
Utils
.
_
.
camelize
(
'set_'
+
(
this
.
options
.
as
||
this
.
target
.
tableName
))
}
}
// the id is in the target table
...
...
@@ -14,15 +18,14 @@ HasOne.prototype.injectAttributes = function() {
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
this
.
source
.
tableName
+
"Id"
,
this
.
options
.
underscored
)
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
Utils
.
_
.
extend
(
this
.
target
.
attributes
,
Utils
.
simplifyAttributes
(
newAttributes
))
return
this
}
HasOne
.
prototype
.
injectGetter
=
function
(
obj
)
{
var
self
=
this
,
accessor
=
Utils
.
_
.
camelize
(
'get_'
+
(
this
.
options
.
as
||
this
.
target
.
tableName
))
var
self
=
this
obj
[
accessor
]
=
function
()
{
obj
[
this
.
accessors
.
get
]
=
function
()
{
var
id
=
obj
.
id
,
where
=
{}
...
...
@@ -34,12 +37,25 @@ HasOne.prototype.injectGetter = function(obj) {
}
HasOne
.
prototype
.
injectSetter
=
function
(
obj
)
{
var
self
=
this
,
accessor
=
Utils
.
_
.
camelize
(
'set_'
+
(
this
.
options
.
as
||
this
.
target
.
tableName
))
var
self
=
this
obj
[
accessor
]
=
function
(
associatedObject
)
{
associatedObject
[
self
.
identifier
]
=
this
.
id
return
associatedObject
.
save
()
obj
[
this
.
accessors
.
set
]
=
function
(
associatedObject
)
{
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
obj
[
self
.
accessors
.
get
]().
on
(
'success'
,
function
(
oldObj
)
{
if
(
oldObj
)
{
oldObj
[
self
.
identifier
]
=
null
oldObj
.
save
()
}
associatedObject
[
self
.
identifier
]
=
obj
.
id
associatedObject
.
save
().
on
(
'success'
,
function
()
{
customEventEmitter
.
emit
(
'success'
,
''
)
}).
on
(
'failure'
,
function
(
err
)
{
customEventEmitter
.
emit
(
'failure'
,
err
)
})
})
})
return
customEventEmitter
}
return
this
...
...
lib/sequelize/utils.js
View file @
7f53f98
...
...
@@ -120,4 +120,7 @@ var Utils = module.exports = {
})
return
result
}
}
\ No newline at end of file
}
Utils
.
CustomEventEmitter
=
function
(
fct
)
{
fct
()
}
Utils
.
addEventEmitter
(
Utils
.
CustomEventEmitter
)
\ No newline at end of file
test/Model/has-one.js
View file @
7f53f98
...
...
@@ -70,5 +70,33 @@ module.exports = {
})
})
})
},
'it should correctly unset the obsolete objects'
:
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
.
hasOne
(
Task
,
{
as
:
'Task'
})
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
)
{
Task
.
create
({
title
:
'another task'
}).
on
(
'success'
,
function
(
task2
)
{
user
.
setTask
(
task
).
on
(
'success'
,
function
()
{
user
.
getTask
().
on
(
'success'
,
function
(
_task
)
{
assert
.
eql
(
task
.
title
,
_task
.
title
)
user
.
setTask
(
task2
).
on
(
'success'
,
function
()
{
user
.
getTask
().
on
(
'success'
,
function
(
_task2
)
{
assert
.
eql
(
task2
.
title
,
_task2
.
title
)
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