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 af1e4dc9
authored
Apr 28, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added class and instance method support
1 parent
cb9c088c
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
23 additions
and
3 deletions
doc/views/index/code/instances/models-3.ejs → doc/views/index/code/instances/update.ejs
doc/views/index/instances.ejs
lib/sequelize/model-definition.js
test/Sequelize/define.js
doc/views/index/code/instances/
models-3
.ejs
→
doc/views/index/code/instances/
update
.ejs
View file @
af1e4dc
// way 1
task.title = 'a very different title now'
task.save(
function()
{})
task.save(
).on('success', function()
{})
// way 2
task.updateAttributes({
title: 'a very different title now'
}
, function()
{})
}
).on('success', function()
{})
doc/views/index/instances.ejs
View file @
af1e4dc
...
...
@@ -13,4 +13,4 @@ To get it stored in the database, use the save method and catch the events, ...,
Now lets change some values and save changes to the database... There are two ways to do that:
<pre><%- koala(".js", partial("code/instances/
models-3
.ejs")) %></pre>
<pre><%- koala(".js", partial("code/instances/
update
.ejs")) %></pre>
lib/sequelize/model-definition.js
View file @
af1e4dc
...
...
@@ -16,9 +16,15 @@ var ModelDefinition = module.exports = function(name, attributes, options) {
this
.
associations
=
{}
this
.
addDefaultAttributes
()
this
.
addOptionalClassMethods
()
}
Utils
.
addEventEmitter
(
ModelDefinition
)
ModelDefinition
.
prototype
.
addOptionalClassMethods
=
function
()
{
var
self
=
this
Utils
.
_
.
each
(
this
.
options
.
classMethods
||
{},
function
(
fct
,
name
)
{
self
[
name
]
=
fct
})
}
ModelDefinition
.
prototype
.
addDefaultAttributes
=
function
()
{
var
defaultAttributes
=
{
id
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
primaryKey
:
true
,
autoIncrement
:
true
}}
,
self
=
this
...
...
@@ -117,6 +123,7 @@ ModelDefinition.prototype.build = function(values, options) {
instance
.
addAttribute
(
name
,
value
)
}
})
Utils
.
_
.
each
(
this
.
options
.
instanceMethods
||
{},
function
(
fct
,
name
)
{
instance
[
name
]
=
fct
})
Utils
.
_
.
each
(
this
.
associations
,
function
(
association
,
associationName
)
{
association
.
injectGetter
(
instance
)
...
...
test/Sequelize/define.js
View file @
af1e4dc
...
...
@@ -62,5 +62,17 @@ module.exports = {
'tablenames should be pluralized if they are not frozen'
:
function
()
{
var
User
=
sequelize
.
define
(
'User'
,
{},
{
freezeTableName
:
false
})
assert
.
eql
(
User
.
tableName
,
'Users'
)
},
'it should add the passed class/instance methods'
:
function
()
{
var
User
=
sequelize
.
define
(
'User'
,
{},
{
classMethods
:
{
doSmth
:
function
(){
return
1
}
},
instanceMethods
:
{
makeItSo
:
function
(){
return
2
}}
})
assert
.
isDefined
(
User
.
doSmth
)
assert
.
eql
(
User
.
doSmth
(),
1
)
assert
.
isUndefined
(
User
.
makeItSo
)
assert
.
isDefined
(
User
.
build
().
makeItSo
)
assert
.
eql
(
User
.
build
().
makeItSo
(),
2
)
}
}
\ 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