Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added basic support for setting instantiated children #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions ampersand-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,16 @@ _.extend(Base.prototype, BBEvents, {


if (!def) {
// if this is a child model or collection
if (this._children[attr] || this._collections[attr]) {
// if this is a child model
if (this._children[attr]) {
if (newVal instanceof this._children[attr]) {
// Get the raw attributes including the session values.
newVal = newVal.getAttributes({ props: true, session: true }, true);
}
this[attr].set(newVal, options);
continue;
// if this is a collection
} else if (this._collections[attr]) {
this[attr].set(newVal, options);
continue;
} else if (extraProperties === 'ignore') {
Expand Down
40 changes: 40 additions & 0 deletions test/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,46 @@ test('listens to child events', function (t) {
first.firstChild.grandChild.name = 'Bob';
});

test('instantiated children can be set', function (t) {
var Child = State.extend({
props: {
id: 'string',
name: 'string'
}
});

var Parent = State.extend({
props: {
id: 'string',
name: 'string'
},
children: {
firstChild: Child,
secondChild: Child
}
});

var child = new Child({
id: 'first-child',
name: 'first-child-name'
});

var parent = new Parent({
id: 'child',
name: 'first-name',
firstChild: child,
secondChild: {
id: 'second-child',
name: 'second-child-name'
}
});

t.plan(2);

t.equal(parent.firstChild.name, 'first-child-name', 'instantiated child should be directly accessible');
t.equal(parent.secondChild.name, 'second-child-name', 'regular child should be directly accessible');
});

test('Should be able to declare derived properties that have nested deps', function (t) {
var GrandChild = State.extend({
props: {
Expand Down