Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
brainfood
/
backbone-nested-models
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
2c606432
authored
2014-04-11 14:46:48 -0500
by
Ean Schuessler
Browse Files
Options
Browse Files
Tag
Download
Plain Diff
Merge branch 'BF-2620' of /home/git/repositories/brainfood/backbone-nested-models
2 parents
0976fcad
adc117a1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
6 deletions
src/scripts/backbone-nested-models.js
test/specs/backbone-nested-models.spec.js
src/scripts/backbone-nested-models.js
View file @
2c60643
...
...
@@ -10,10 +10,30 @@ define(
)
{
'use strict'
;
function
extractNestedErrors
(
errors
)
{
var
result
=
{};
for
(
var
key
in
errors
)
{
var
value
=
errors
[
key
];
if
(
value
.
errors
)
{
value
=
extractNestedErrors
(
value
.
errors
);
}
result
[
key
]
=
value
;
}
return
result
;
}
function
validateNestedValue
(
attrValue
,
attrName
)
{
attrValue
.
validate
();
var
errors
=
attrValue
.
validate
();
var
isValid
=
attrValue
.
isValid
();
return
isValid
?
null
:
(
attrName
+
' is invalid'
);
if
(
isValid
)
{
return
null
;
}
var
r
=
{};
r
.
toString
=
function
()
{
return
attrName
+
' is invalid'
;
};
r
.
errors
=
errors
;
return
r
;
}
function
updateValidation
(
model
)
{
...
...
@@ -120,6 +140,7 @@ define(
}
var
NestedModels
=
{
extractNestedErrors
:
extractNestedErrors
,
validateNestedValue
:
validateNestedValue
,
wrapSetFunction
:
wrapSetFunction
,
...
...
test/specs/backbone-nested-models.spec.js
View file @
2c60643
...
...
@@ -290,10 +290,15 @@ define(function(require) {
expect
(
top
.
isValid
()).
toBeUndefined
();
result
=
top
.
validate
();
expect
(
result
).
not
.
toBeNull
();
expect
(
result
.
nested
).
not
.
toEqual
(
jasmine
.
any
(
String
));
expect
(
result
.
nested
.
toString
()).
toEqual
(
'nested is invalid'
);
result
=
NestedModels
.
extractNestedErrors
(
result
);
rKeys
=
_
.
keys
(
result
).
sort
();
expect
(
rKeys
).
toEqual
([
'name'
,
'nested'
]);
expect
(
result
.
name
).
toEqual
(
jasmine
.
any
(
String
));
expect
(
result
.
nested
).
toEqual
(
jasmine
.
any
(
String
));
expect
(
_
.
keys
(
result
.
nested
).
sort
()).
toEqual
([
'address1'
,
'countryGeoId'
]);
expect
(
result
.
nested
.
address1
).
toEqual
(
jasmine
.
any
(
String
));
expect
(
result
.
nested
.
countryGeoId
).
toEqual
(
jasmine
.
any
(
String
));
expect
(
top
.
isValid
()).
toBe
(
false
);
if
(
checkCounts
)
{
expect
(
top
.
counts
).
toEqual
({
set
:
1
,
isValid
:
2
,
validate
:
1
});
...
...
@@ -335,10 +340,15 @@ define(function(require) {
];
result
=
top
.
validate
();
expect
(
result
).
not
.
toBeNull
();
expect
(
result
.
nested
).
not
.
toEqual
(
jasmine
.
any
(
String
));
expect
(
result
.
nested
.
toString
()).
toEqual
(
'nested is invalid'
);
result
=
NestedModels
.
extractNestedErrors
(
result
);
rKeys
=
_
.
keys
(
result
).
sort
();
expect
(
rKeys
).
toEqual
([
'name'
,
'nested'
]);
expect
(
result
.
name
).
toEqual
(
'name IS required'
);
expect
(
result
.
nested
).
toEqual
(
jasmine
.
any
(
String
));
expect
(
_
.
keys
(
result
.
nested
).
sort
()).
toEqual
([
'address1'
,
'countryGeoId'
]);
expect
(
result
.
nested
.
address1
).
toEqual
(
jasmine
.
any
(
String
));
expect
(
result
.
nested
.
countryGeoId
).
toEqual
(
jasmine
.
any
(
String
));
if
(
checkCounts
)
{
expect
(
top
.
counts
).
toEqual
({
set
:
1
,
isValid
:
2
,
validate
:
4
});
}
else
{
...
...
@@ -348,10 +358,14 @@ define(function(require) {
top
.
get
(
'nested'
).
set
(
'address1'
,
'1234 Main St.'
);
result
=
top
.
validate
();
expect
(
result
).
not
.
toBeNull
();
expect
(
result
.
nested
).
not
.
toEqual
(
jasmine
.
any
(
String
));
expect
(
result
.
nested
.
toString
()).
toEqual
(
'nested is invalid'
);
result
=
NestedModels
.
extractNestedErrors
(
result
);
rKeys
=
_
.
keys
(
result
).
sort
();
expect
(
rKeys
).
toEqual
([
'name'
,
'nested'
]);
expect
(
result
.
name
).
toEqual
(
'name IS required'
);
expect
(
result
.
nested
).
toEqual
(
jasmine
.
any
(
String
));
expect
(
_
.
keys
(
result
.
nested
).
sort
()).
toEqual
([
'countryGeoId'
]);
expect
(
result
.
nested
.
countryGeoId
).
toEqual
(
jasmine
.
any
(
String
));
if
(
checkCounts
)
{
expect
(
top
.
counts
).
toEqual
({
set
:
1
,
isValid
:
2
,
validate
:
5
});
}
else
{
...
...
@@ -361,10 +375,14 @@ define(function(require) {
top
.
get
(
'nested'
).
set
(
'countryGeoId'
,
'foobar'
);
result
=
top
.
validate
();
expect
(
result
).
not
.
toBeNull
();
expect
(
result
.
nested
).
not
.
toEqual
(
jasmine
.
any
(
String
));
expect
(
result
.
nested
.
toString
()).
toEqual
(
'nested is invalid'
);
result
=
NestedModels
.
extractNestedErrors
(
result
);
rKeys
=
_
.
keys
(
result
).
sort
();
expect
(
rKeys
).
toEqual
([
'name'
,
'nested'
]);
expect
(
result
.
name
).
toEqual
(
'name IS required'
);
expect
(
result
.
nested
).
toEqual
(
jasmine
.
any
(
String
));
expect
(
_
.
keys
(
result
.
nested
).
sort
()).
toEqual
([
'countryGeoId'
]);
expect
(
result
.
nested
.
countryGeoId
).
toEqual
(
jasmine
.
any
(
String
));
if
(
checkCounts
)
{
expect
(
top
.
counts
).
toEqual
({
set
:
1
,
isValid
:
2
,
validate
:
6
});
}
else
{
...
...
Please
register
or
sign in
to post a comment