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

Force child states to parse on initial data #190

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions ampersand-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,15 +454,15 @@ assign(Base.prototype, Events, {
var coll;
if (!this._collections) return;
for (coll in this._collections) {
this[coll] = new this._collections[coll](null, {parent: this});
this[coll] = new this._collections[coll](null, {parent: this, parse: true});
}
},

_initChildren: function () {
var child;
if (!this._children) return;
for (child in this._children) {
this[child] = new this._children[child]({}, {parent: this});
this[child] = new this._children[child]({}, {parent: this, parse: true});
this.listenTo(this[child], 'all', this._getEventBubblingHandler(child));
}
},
Expand Down
60 changes: 60 additions & 0 deletions test/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -1653,3 +1653,63 @@ test('throw helpful error if trying to extend with `prop` that already is define

t.end();
});

test('#146 consistently apply parse for children and collections', function (t) {
var Person = State.extend({
props: {
name: 'string',
parsed: {
type: 'number',
default: 0
}
},
// parse counts itself as parsed when ran
parse: function (attrs) {
if (typeof attrs.parsed !== 'number') {
attrs.parsed = 0;
}
attrs.parsed += 1;
return attrs;
}
});

var Friends = Collection.extend({
model: Person
});

var ParentObj = State.extend({
children: {
child: Person
},
collections: {
friends: Friends
}
});

var Parents = Collection.extend({
model: ParentObj
});

var parents = new Parents();

parents.add([
{
name: 'first',
child: {
name: 'mary'
},
friends: [
{
name: 'bob'
}
]
}
], {parse: true});

console.log(parents.at(0).friends.length);

t.equal(parents.at(0).child.parsed, 1, 'child should have been parsed');
t.equal(parents.at(0).friends.at(0).parsed, 1, 'friend collection items should have been parsed');

t.end();
});