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
88d84fee
authored
2014-04-10 12:09:44 -0500
by
Adam Heath
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Issue #2608: Add support for parsing incoming pojo into an existing
model child instance.
1 parent
3a21b2ec
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
2 deletions
src/scripts/backbone-nested-models.js
test/specs/backbone-nested-models.spec.js
src/scripts/backbone-nested-models.js
View file @
88d84fe
...
...
@@ -83,13 +83,18 @@ define(
(
attrs
=
{})[
key
]
=
val
;
}
if
(
options
&&
options
.
merge
)
{
nestedOptions
=
{
silent
:
false
,
merge
:
true
};
nestedOptions
=
{
silent
:
false
,
merge
:
true
,
parse
:
options
.
parse
};
for
(
attr
in
attrs
)
{
curVal
=
this
.
get
(
attr
);
newVal
=
attrs
[
attr
];
if
(
curVal
instanceof
Backbone
.
Model
&&
newVal
instanceof
Backbone
.
Model
)
{
if
(
curVal
instanceof
Backbone
.
Model
)
{
if
(
newVal
instanceof
Backbone
.
Model
)
{
delete
attrs
[
attr
];
curVal
.
set
(
newVal
.
attributes
,
nestedOptions
);
}
else
if
(
options
.
parse
&&
typeof
newVal
===
'object'
)
{
delete
attrs
[
attr
];
curVal
.
set
(
newVal
,
nestedOptions
);
}
}
}
}
...
...
test/specs/backbone-nested-models.spec.js
View file @
88d84fe
...
...
@@ -189,6 +189,33 @@ define(function(require) {
}
else
{
expect
(
top
.
counts
).
toBeUndefined
();
}
result
=
top
.
set
(
'nested'
,
{
address1
:
'1234 Main St.'
},
{
merge
:
true
,
parse
:
true
});
expect
(
result
).
toBe
(
top
);
var
cidsSetnestedpojoparse
=
extractCids
(
top
);
expect
(
cidsSetnestedmerge
.
top
).
toEqual
(
cidsSetnestedpojoparse
.
top
);
expect
(
cidsSetnestedmerge
.
nested
).
toEqual
(
cidsSetnestedpojoparse
.
nested
);
expect
(
top
.
get
(
'name'
)).
toEqual
(
'value'
);
expect
(
top
.
get
(
'nested'
).
get
(
'address1'
)).
toEqual
(
'1234 Main St.'
);
if
(
checkCounts
)
{
expect
(
top
.
counts
).
toEqual
({
set
:
7
});
}
else
{
expect
(
top
.
counts
).
toBeUndefined
();
}
result
=
top
.
set
(
'nested'
,
{
address1
:
'4321 Main St.'
},
{
merge
:
true
});
expect
(
result
).
toBe
(
top
);
var
cidsSetnestedpojo
=
extractCids
(
top
);
expect
(
cidsSetnestedpojoparse
.
top
).
toEqual
(
cidsSetnestedpojo
.
top
);
expect
(
cidsSetnestedpojo
.
nested
).
toBeUndefined
();
expect
(
top
.
get
(
'name'
)).
toEqual
(
'value'
);
expect
(
top
.
get
(
'nested'
)).
not
.
toEqual
(
jasmine
.
any
(
Backbone
.
Model
));
expect
(
top
.
get
(
'nested'
).
address1
).
toEqual
(
'4321 Main St.'
);
if
(
checkCounts
)
{
expect
(
top
.
counts
).
toEqual
({
set
:
8
});
}
else
{
expect
(
top
.
counts
).
toBeUndefined
();
}
}
it
(
'set-mixin-plain'
,
function
()
{
testSet
.
call
(
this
,
NestedModels
.
mixin
(
Top
),
false
);
...
...
Please
register
or
sign in
to post a comment