Skip to content

Commit

Permalink
Merge 8bcc977 into c23194e
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek committed Oct 30, 2015
2 parents c23194e + 8bcc977 commit 327906c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ampersand-state.js
Expand Up @@ -450,15 +450,15 @@ assign(Base.prototype, Events, {
var coll;
if (!this._collections) return;
for (coll in this._collections) {
this._safeSet(coll, new this._collections[coll](null, {parent: this}));
this._safeSet(coll, new this._collections[coll](null, {parent: this, parse: true}));
}
},

_initChildren: function () {
var child;
if (!this._children) return;
for (child in this._children) {
this._safeSet(child, new this._children[child]({}, {parent: this}));
this._safeSet(child, new this._children[child]({}, {parent: this, parse: true}));
this.listenTo(this[child], 'all', this._getEventBubblingHandler(child));
}
},
Expand Down
58 changes: 58 additions & 0 deletions test/full.js
Expand Up @@ -1814,3 +1814,61 @@ test('toJSON should serialize customType props - issue #197', function(t) {

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});

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();
});

0 comments on commit 327906c

Please sign in to comment.