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 ea5c4337
authored
Nov 09, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored associations mixin
1 parent
9854b2f7
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
36 additions
and
38 deletions
lib/associations/mixin.js
lib/model-definition.js
lib/model.js
lib/associations/mixin.js
View file @
ea5c433
var
Utils
=
require
(
"./../utils"
)
/*
Defines Mixin for all models.
*/
var
Associations
=
module
.
exports
=
{
classMethods
:
{
hasOne
:
function
(
associatedModel
,
options
)
{
// the id is in the foreign table
var
HasOne
=
require
(
'./has-one'
)
var
association
=
new
HasOne
(
this
,
associatedModel
,
Utils
.
_
.
extend
((
options
||
{}),
this
.
options
))
this
.
associations
[
association
.
associationAccessor
]
=
association
.
injectAttributes
()
},
belongsTo
:
function
(
associatedModel
,
options
)
{
// the id is in this table
var
BelongsTo
=
require
(
"./belongs-to"
)
var
association
=
new
BelongsTo
(
this
,
associatedModel
,
Utils
.
_
.
extend
((
options
||
{}),
this
.
options
))
this
.
associations
[
association
.
associationAccessor
]
=
association
.
injectAttributes
()
},
hasMany
:
function
(
associatedModel
,
options
)
{
// the id is in the foreign table or in a connecting table
var
HasMany
=
require
(
"./has-many"
)
var
association
=
new
HasMany
(
this
,
associatedModel
,
Utils
.
_
.
extend
((
options
||
{}),
this
.
options
))
this
.
associations
[
association
.
associationAccessor
]
=
association
.
injectAttributes
()
}
},
instanceMethods
:
{
}
/* Defines Mixin for all models. */
var
Mixin
=
module
.
exports
=
function
(){}
Mixin
.
hasOne
=
function
(
associatedModel
,
options
)
{
// the id is in the foreign table
var
HasOne
=
require
(
'./has-one'
)
var
association
=
new
HasOne
(
this
,
associatedModel
,
Utils
.
_
.
extend
((
options
||
{}),
this
.
options
))
this
.
associations
[
association
.
associationAccessor
]
=
association
.
injectAttributes
()
}
Mixin
.
belongsTo
=
function
(
associatedModel
,
options
)
{
// the id is in this table
var
BelongsTo
=
require
(
"./belongs-to"
)
var
association
=
new
BelongsTo
(
this
,
associatedModel
,
Utils
.
_
.
extend
((
options
||
{}),
this
.
options
))
this
.
associations
[
association
.
associationAccessor
]
=
association
.
injectAttributes
()
}
Mixin
.
hasMany
=
function
(
associatedModel
,
options
)
{
// the id is in the foreign table or in a connecting table
var
HasMany
=
require
(
"./has-many"
)
var
association
=
new
HasMany
(
this
,
associatedModel
,
Utils
.
_
.
extend
((
options
||
{}),
this
.
options
))
this
.
associations
[
association
.
associationAccessor
]
=
association
.
injectAttributes
()
}
/* example for instance methods:
Mixin.prototype.test = function() {
console.log('asd')
}
*/
lib/model-definition.js
View file @
ea5c433
...
...
@@ -167,6 +167,15 @@ ModelDefinition.prototype.__defineGetter__('hasPrimaryKeys', function() {
// private
ModelDefinition
.
prototype
.
__query
=
function
()
{
var
args
=
Utils
.
_
.
map
(
arguments
,
function
(
arg
,
_
)
{
return
arg
})
,
s
=
this
.
modelManager
.
sequelize
// add this as the second argument
if
(
arguments
.
length
==
1
)
args
.
push
(
this
)
return
s
.
query
.
apply
(
s
,
args
)
}
ModelDefinition
.
prototype
.
__addOptionalClassMethods
=
function
()
{
var
self
=
this
Utils
.
_
.
each
(
this
.
options
.
classMethods
||
{},
function
(
fct
,
name
)
{
self
[
name
]
=
fct
})
...
...
@@ -204,14 +213,4 @@ ModelDefinition.prototype.__findAutoIncrementField = function() {
})
}
ModelDefinition
.
prototype
.
__query
=
function
()
{
var
args
=
Utils
.
_
.
map
(
arguments
,
function
(
arg
,
_
)
{
return
arg
})
,
s
=
this
.
modelManager
.
sequelize
// add this as the second argument
if
(
arguments
.
length
==
1
)
args
.
push
(
this
)
return
s
.
query
.
apply
(
s
,
args
)
}
Utils
.
_
.
map
(
require
(
"./associations/mixin"
).
classMethods
,
function
(
fct
,
name
)
{
ModelDefinition
.
prototype
[
name
]
=
fct
})
Utils
.
_
.
extend
(
ModelDefinition
.
prototype
,
require
(
"./associations/mixin"
))
lib/model.js
View file @
ea5c433
...
...
@@ -172,4 +172,4 @@ Model.prototype.equalsOneOf = function(others) {
}
/* Add the instance methods to Model */
Utils
.
_
.
map
(
Mixin
.
instanceMethods
,
function
(
fct
,
name
)
{
Model
.
prototype
[
name
]
=
fct
}
)
Utils
.
_
.
extend
(
Model
.
prototype
,
Mixin
.
prototype
)
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