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 22635d60
authored
Apr 26, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
default values for freshly builded objects
1 parent
ebd36605
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
2 deletions
lib/sequelize/model-definition.js
test/Model/build.js
lib/sequelize/model-definition.js
View file @
22635d6
...
@@ -11,6 +11,7 @@ var ModelDefinition = module.exports = function(name, attributes, options) {
...
@@ -11,6 +11,7 @@ var ModelDefinition = module.exports = function(name, attributes, options) {
this
.
name
=
name
this
.
name
=
name
this
.
tableName
=
this
.
options
.
freezeTableName
?
name
:
Utils
.
pluralize
(
name
)
this
.
tableName
=
this
.
options
.
freezeTableName
?
name
:
Utils
.
pluralize
(
name
)
this
.
attributes
=
Utils
.
simplifyAttributes
(
attributes
)
this
.
attributes
=
Utils
.
simplifyAttributes
(
attributes
)
this
.
rawAttributes
=
attributes
this
.
modelManager
=
null
// defined by model-manager during addModel
this
.
modelManager
=
null
// defined by model-manager during addModel
this
.
associations
=
{}
this
.
associations
=
{}
...
@@ -106,8 +107,13 @@ ModelDefinition.prototype.build = function(values) {
...
@@ -106,8 +107,13 @@ ModelDefinition.prototype.build = function(values) {
Utils
.
_
.
map
(
this
.
attributes
,
function
(
definition
,
name
)
{
Utils
.
_
.
map
(
this
.
attributes
,
function
(
definition
,
name
)
{
if
(
typeof
instance
[
name
]
==
'undefined'
)
{
if
(
typeof
instance
[
name
]
==
'undefined'
)
{
instance
[
name
]
=
null
var
value
=
null
instance
.
addAttribute
(
name
,
null
)
if
(
self
.
rawAttributes
.
hasOwnProperty
(
name
))
value
=
self
.
rawAttributes
[
name
].
defaultValue
||
null
instance
[
name
]
=
value
instance
.
addAttribute
(
name
,
value
)
}
}
})
})
...
...
test/Model/build.js
View file @
22635d6
...
@@ -22,5 +22,17 @@ module.exports = {
...
@@ -22,5 +22,17 @@ module.exports = {
exit
(
function
(){})
exit
(
function
(){})
})
})
})
})
},
'build should fill the object with default values'
:
function
()
{
var
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
{
dataType
:
Sequelize
.
STRING
,
defaultValue
:
'a task!'
},
foo
:
{
dataType
:
Sequelize
.
INTEGER
,
defaultValue
:
2
},
bar
:
{
dataType
:
Sequelize
.
DATE
},
foobar
:
{
dataType
:
Sequelize
.
TEXT
,
defaultValue
:
'asd'
}
})
assert
.
eql
(
Task
.
build
().
title
,
'a task!'
)
assert
.
eql
(
Task
.
build
().
foo
,
2
)
assert
.
eql
(
Task
.
build
().
bar
,
null
)
assert
.
eql
(
Task
.
build
().
foobar
,
'asd'
)
}
}
}
}
\ 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