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 4dbbb89b
authored
Nov 12, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
private method scoping for associations
1 parent
3e57b469
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
87 additions
and
68 deletions
lib/associations/belongs-to.js
lib/associations/has-many-double-linked.js
lib/associations/has-many-single-linked.js
lib/associations/has-many.js
lib/associations/has-one.js
lib/associations/belongs-to.js
View file @
4dbbb89
var
Utils
=
require
(
"./../utils"
)
,
DataTypes
=
require
(
'./../data-types'
)
var
BelongsTo
=
module
.
exports
=
function
(
srcModel
,
targetModel
,
options
)
{
module
.
exports
=
(
function
()
{
var
BelongsTo
=
function
(
srcModel
,
targetModel
,
options
)
{
this
.
source
=
srcModel
this
.
target
=
targetModel
this
.
options
=
options
...
...
@@ -13,10 +14,10 @@ var BelongsTo = module.exports = function(srcModel, targetModel, options) {
this
.
associationAccessor
=
this
.
isSelfAssociation
?
Utils
.
combineTableNames
(
this
.
target
.
tableName
,
this
.
options
.
as
||
this
.
target
.
tableName
)
:
this
.
target
.
tableName
}
}
// the id is in the source table
BelongsTo
.
prototype
.
injectAttributes
=
function
()
{
// the id is in the source table
BelongsTo
.
prototype
.
injectAttributes
=
function
()
{
var
newAttributes
=
{}
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
target
.
tableName
)
+
"Id"
,
this
.
source
.
options
.
underscored
)
...
...
@@ -24,9 +25,9 @@ BelongsTo.prototype.injectAttributes = function() {
Utils
.
_
.
extend
(
this
.
source
.
attributes
,
Utils
.
simplifyAttributes
(
newAttributes
))
return
this
}
}
BelongsTo
.
prototype
.
injectGetter
=
function
(
obj
)
{
BelongsTo
.
prototype
.
injectGetter
=
function
(
obj
)
{
var
self
=
this
,
accessor
=
Utils
.
_
.
camelize
(
'get_'
+
(
this
.
options
.
as
||
Utils
.
singularize
(
this
.
target
.
tableName
)))
...
...
@@ -36,9 +37,9 @@ BelongsTo.prototype.injectGetter = function(obj) {
}
return
this
}
}
BelongsTo
.
prototype
.
injectSetter
=
function
(
obj
)
{
BelongsTo
.
prototype
.
injectSetter
=
function
(
obj
)
{
var
self
=
this
,
accessor
=
Utils
.
_
.
camelize
(
'set_'
+
(
this
.
options
.
as
||
Utils
.
singularize
(
this
.
target
.
tableName
)))
...
...
@@ -48,4 +49,7 @@ BelongsTo.prototype.injectSetter = function(obj) {
}
return
this
}
\ No newline at end of file
}
return
BelongsTo
})()
lib/associations/has-many-double-linked.js
View file @
4dbbb89
var
Utils
=
require
(
'./../utils'
)
var
HasManyDoubleLinked
=
module
.
exports
=
function
(
definition
,
instance
)
{
module
.
exports
=
(
function
()
{
var
HasManyDoubleLinked
=
function
(
definition
,
instance
)
{
this
.
__definition
=
definition
this
.
instance
=
instance
}
}
HasManyDoubleLinked
.
prototype
.
injectGetter
=
function
()
{
HasManyDoubleLinked
.
prototype
.
injectGetter
=
function
()
{
var
self
=
this
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
...
...
@@ -29,9 +30,37 @@ HasManyDoubleLinked.prototype.injectGetter = function() {
})
return
customEventEmitter
.
run
()
}
}
HasManyDoubleLinked
.
prototype
.
injectSetter
=
function
(
emitter
,
oldAssociations
,
newAssociations
)
{
var
self
=
this
destroyObsoleteAssociations
.
call
(
this
,
oldAssociations
,
newAssociations
)
.
on
(
'failure'
,
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
.
on
(
'success'
,
function
()
{
var
chainer
=
new
Utils
.
QueryChainer
,
association
=
self
.
__definition
.
target
.
associations
[
self
.
__definition
.
associationAccessor
]
,
foreignIdentifier
=
association
.
isSelfAssociation
?
association
.
foreignIdentifier
:
association
.
identifier
,
unassociatedObjects
=
newAssociations
.
filter
(
function
(
obj
)
{
return
!
obj
.
equalsOneOf
(
oldAssociations
)
})
unassociatedObjects
.
forEach
(
function
(
unassociatedObject
)
{
var
attributes
=
{}
attributes
[
self
.
__definition
.
identifier
]
=
self
.
instance
.
id
attributes
[
foreignIdentifier
]
=
unassociatedObject
.
id
chainer
.
add
(
self
.
__definition
.
connectorModel
.
create
(
attributes
))
})
chainer
.
run
()
.
on
(
'success'
,
function
()
{
emitter
.
emit
(
'success'
,
newAssociations
)
})
.
on
(
'failure'
,
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
})
}
// private
HasManyDoubleLinked
.
prototype
.
destroyObsoleteAssociations
=
function
(
oldAssociations
,
newAssociations
)
{
var
destroyObsoleteAssociations
=
function
(
oldAssociations
,
newAssociations
)
{
var
self
=
this
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
...
...
@@ -64,30 +93,7 @@ HasManyDoubleLinked.prototype.destroyObsoleteAssociations = function(oldAssociat
})
})
return
emitter
.
run
()
}
HasManyDoubleLinked
.
prototype
.
injectSetter
=
function
(
emitter
,
oldAssociations
,
newAssociations
)
{
var
self
=
this
this
.
destroyObsoleteAssociations
(
oldAssociations
,
newAssociations
)
.
on
(
'failure'
,
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
.
on
(
'success'
,
function
()
{
var
chainer
=
new
Utils
.
QueryChainer
,
association
=
self
.
__definition
.
target
.
associations
[
self
.
__definition
.
associationAccessor
]
,
foreignIdentifier
=
association
.
isSelfAssociation
?
association
.
foreignIdentifier
:
association
.
identifier
,
unassociatedObjects
=
newAssociations
.
filter
(
function
(
obj
)
{
return
!
obj
.
equalsOneOf
(
oldAssociations
)
})
unassociatedObjects
.
forEach
(
function
(
unassociatedObject
)
{
var
attributes
=
{}
attributes
[
self
.
__definition
.
identifier
]
=
self
.
instance
.
id
attributes
[
foreignIdentifier
]
=
unassociatedObject
.
id
chainer
.
add
(
self
.
__definition
.
connectorModel
.
create
(
attributes
))
})
}
chainer
.
run
()
.
on
(
'success'
,
function
()
{
emitter
.
emit
(
'success'
,
newAssociations
)
})
.
on
(
'failure'
,
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
})
}
return
HasManyDoubleLinked
})()
lib/associations/has-many-single-linked.js
View file @
4dbbb89
var
Utils
=
require
(
'./../utils'
)
var
HasManySingleLinked
=
module
.
exports
=
function
(
definition
,
instance
)
{
module
.
exports
=
(
function
()
{
var
HasManySingleLinked
=
function
(
definition
,
instance
)
{
this
.
__definition
=
definition
this
.
instance
=
instance
}
}
HasManySingleLinked
.
prototype
.
injectGetter
=
function
()
{
HasManySingleLinked
.
prototype
.
injectGetter
=
function
()
{
var
where
=
{}
where
[
this
.
__definition
.
identifier
]
=
this
.
instance
.
id
return
this
.
__definition
.
target
.
findAll
({
where
:
where
})
}
}
HasManySingleLinked
.
prototype
.
injectSetter
=
function
(
emitter
,
oldAssociations
,
newAssociations
)
{
HasManySingleLinked
.
prototype
.
injectSetter
=
function
(
emitter
,
oldAssociations
,
newAssociations
)
{
var
self
=
this
// clear the old associations
...
...
@@ -31,4 +32,7 @@ HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations,
.
run
()
.
on
(
'success'
,
function
()
{
emitter
.
emit
(
'success'
,
newAssociations
)
})
.
on
(
'failure'
,
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
}
}
return
HasManySingleLinked
})()
lib/associations/has-many.js
View file @
4dbbb89
...
...
@@ -4,7 +4,8 @@ var Utils = require("./../utils")
var
HasManySingleLinked
=
require
(
"./has-many-single-linked"
)
,
HasManyMultiLinked
=
require
(
"./has-many-double-linked"
)
var
HasMany
=
module
.
exports
=
function
(
srcModel
,
targetModel
,
options
)
{
module
.
exports
=
(
function
()
{
var
HasMany
=
function
(
srcModel
,
targetModel
,
options
)
{
this
.
source
=
srcModel
this
.
target
=
targetModel
this
.
options
=
options
...
...
@@ -23,11 +24,11 @@ var HasMany = module.exports = function(srcModel, targetModel, options) {
add
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'add_'
+
as
)),
remove
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'remove_'
+
as
))
}
}
}
// the id is in the target table
// or in an extra table which connects two tables
HasMany
.
prototype
.
injectAttributes
=
function
()
{
// the id is in the target table
// or in an extra table which connects two tables
HasMany
.
prototype
.
injectAttributes
=
function
()
{
var
multiAssociation
=
this
.
target
.
associations
.
hasOwnProperty
(
this
.
associationAccessor
)
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
source
.
tableName
)
+
"Id"
,
this
.
options
.
underscored
)
...
...
@@ -58,9 +59,9 @@ HasMany.prototype.injectAttributes = function() {
}
return
this
}
}
HasMany
.
prototype
.
injectGetter
=
function
(
obj
)
{
HasMany
.
prototype
.
injectGetter
=
function
(
obj
)
{
var
self
=
this
obj
[
this
.
accessors
.
get
]
=
function
()
{
...
...
@@ -69,9 +70,9 @@ HasMany.prototype.injectGetter = function(obj) {
}
return
this
}
}
HasMany
.
prototype
.
injectSetter
=
function
(
obj
)
{
HasMany
.
prototype
.
injectSetter
=
function
(
obj
)
{
var
self
=
this
obj
[
this
.
accessors
.
set
]
=
function
(
newAssociatedObjects
)
{
...
...
@@ -124,4 +125,7 @@ HasMany.prototype.injectSetter = function(obj) {
}
return
this
}
\ No newline at end of file
}
return
HasMany
})()
lib/associations/has-one.js
View file @
4dbbb89
var
Utils
=
require
(
"./../utils"
)
,
DataTypes
=
require
(
'./../data-types'
)
var
HasOne
=
module
.
exports
=
function
(
srcModel
,
targetModel
,
options
)
{
module
.
exports
=
(
function
()
{
var
HasOne
=
function
(
srcModel
,
targetModel
,
options
)
{
this
.
source
=
srcModel
this
.
target
=
targetModel
this
.
options
=
options
...
...
@@ -18,10 +19,10 @@ var HasOne = module.exports = function(srcModel, targetModel, options) {
get
:
Utils
.
_
.
camelize
(
'get_'
+
(
this
.
options
.
as
||
Utils
.
singularize
(
this
.
target
.
tableName
))),
set
:
Utils
.
_
.
camelize
(
'set_'
+
(
this
.
options
.
as
||
Utils
.
singularize
(
this
.
target
.
tableName
)))
}
}
}
// the id is in the target table
HasOne
.
prototype
.
injectAttributes
=
function
()
{
// the id is in the target table
HasOne
.
prototype
.
injectAttributes
=
function
()
{
var
newAttributes
=
{}
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
source
.
tableName
)
+
"Id"
,
this
.
options
.
underscored
)
...
...
@@ -29,9 +30,9 @@ HasOne.prototype.injectAttributes = function() {
Utils
.
_
.
extend
(
this
.
target
.
attributes
,
Utils
.
simplifyAttributes
(
newAttributes
))
return
this
}
}
HasOne
.
prototype
.
injectGetter
=
function
(
obj
)
{
HasOne
.
prototype
.
injectGetter
=
function
(
obj
)
{
var
self
=
this
obj
[
this
.
accessors
.
get
]
=
function
()
{
...
...
@@ -43,9 +44,9 @@ HasOne.prototype.injectGetter = function(obj) {
}
return
this
}
}
HasOne
.
prototype
.
injectSetter
=
function
(
obj
)
{
HasOne
.
prototype
.
injectSetter
=
function
(
obj
)
{
var
self
=
this
obj
[
this
.
accessors
.
set
]
=
function
(
associatedObject
)
{
...
...
@@ -66,4 +67,7 @@ HasOne.prototype.injectSetter = function(obj) {
}
return
this
}
\ No newline at end of file
}
return
HasOne
})()
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