From 932f3762bf29c35dc36ef2721c1892d6e63835b4 Mon Sep 17 00:00:00 2001 From: Henrik Joreteg Date: Thu, 26 Feb 2015 13:00:28 -0800 Subject: [PATCH 1/2] a failing test for problem described in #146. No solution, yet. --- test/full.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/test/full.js b/test/full.js index e021b63..472fab7 100644 --- a/test/full.js +++ b/test/full.js @@ -1814,3 +1814,63 @@ 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}); + + 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(); +}); From 8bcc9774e2e8543bdc4d494c8e7d1ab568dd2363 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Fri, 30 Oct 2015 18:26:11 +0100 Subject: [PATCH 2/2] Fixed outstanding collections/children parsing tests --- ampersand-state.js | 4 ++-- test/full.js | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/ampersand-state.js b/ampersand-state.js index 67cf0f9..9cc3273 100644 --- a/ampersand-state.js +++ b/ampersand-state.js @@ -450,7 +450,7 @@ 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})); } }, @@ -458,7 +458,7 @@ assign(Base.prototype, Events, { 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)); } }, diff --git a/test/full.js b/test/full.js index 472fab7..240bcf5 100644 --- a/test/full.js +++ b/test/full.js @@ -1827,7 +1827,7 @@ test('#146 consistently apply parse for children and collections', function (t) // parse counts itself as parsed when ran parse: function (attrs) { if (typeof attrs.parsed !== 'number') { - attrs.parsed = 0; + attrs.parsed = 0; } attrs.parsed += 1; return attrs; @@ -1867,8 +1867,6 @@ test('#146 consistently apply parse for children and collections', function (t) } ], {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');