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

children should not have changes when data is set during parent instantiation #120

Open
wants to merge 3 commits 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
21 changes: 14 additions & 7 deletions ampersand-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ function Base(attrs, options) {
this.parent = options.parent;
this.collection = options.collection;
this._keyTree = new KeyTree();
this._initCollections();
this._initChildren();
this._initCollections(attrs, _.pick(options, 'parse'));
this._initChildren(attrs, _.pick(options, 'parse'));
this._cache = {};
this._previousAttributes = {};
if (attrs) this.set(attrs, _.extend({silent: true, initial: true}, options));
if (attrs) {
// omit children and collection attributes since we instantiate
// them with attrs during _initCollections and _initChildren
attrs = _.omit(attrs, function(attr, name) { return (name in this._collections) || (name in this._children); }, this);
this.set(attrs, _.extend({silent: true, initial: true}, options));
}
this._changed = {};
if (this._derived) this._initDerived();
if (options.init !== false) this.initialize.apply(this, arguments);
Expand Down Expand Up @@ -427,19 +432,21 @@ _.extend(Base.prototype, BBEvents, {
}
},

_initCollections: function () {
_initCollections: function (attrs, options) {
var coll;
if (!this._collections) return;
options || (options = {});
for (coll in this._collections) {
this[coll] = new this._collections[coll](null, {parent: this});
this[coll] = new this._collections[coll](attrs ? attrs[coll] : null, _.extend({parent: this}, options));
}
},

_initChildren: function () {
_initChildren: function (attrs, options) {
var child;
if (!this._children) return;
options || (options = {});
for (child in this._children) {
this[child] = new this._children[child]({}, {parent: this});
this[child] = new this._children[child](attrs ? attrs[child] : null, _.extend({parent: this}, options));
this.listenTo(this[child], 'all', this._getEventBubblingHandler(child));
}
},
Expand Down
53 changes: 53 additions & 0 deletions test/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,59 @@ test('children and collections should be instantiated', function (t) {
t.end();
});

test('parent instantiation with children', function (t) {
var Widget = State.extend({
props: {
title: 'string',
size: 'string'
},
parse: function(attrs) {
attrs.title = attrs.serverTitle;
delete attrs.serverTitle;
return attrs;
}
});
var Parent = State.extend({
children: {
widget: Widget
}
});
var parent = new Parent({ widget: { serverTitle: 'foo', size: 'lg' } }, { parse: true });

t.equal(parent.hasChanged(), false, 'parent should not register as having changes');
t.equal(parent.widget.hasChanged(), false, 'child should not register as having changes');
t.equal(parent.widget.title, 'foo', 'child should be instantiated with parse option same as parent');
t.end();
});

test('parent instantiation with collections', function (t) {
var Widget = State.extend({
props: {
title: 'string',
size: 'string'
},
parse: function(attrs) {
attrs.title = attrs.serverTitle;
delete attrs.serverTitle;
return attrs;
}
});
var WidgetCollection = Collection.extend({
model: Widget
});
var Parent = State.extend({
collections: {
widgets: WidgetCollection
}
});
var parent = new Parent({ widgets: [{ serverTitle: 'foo', size: 'lg' }] }, { parse: true });

t.equal(parent.hasChanged(), false, 'parent should not register as having changes');
t.equal(parent.widgets.at(0).hasChanged(), false, 'collection models should not register as having changes');
t.equal(parent.widgets.at(0).title, 'foo', 'collection models should be instantiated with same parse option as parent');
t.end();
});

test('issue #82, child collections should not be cleared if they add data to themselves when instantiated', function (t) {
var Widget = State.extend({
props: {
Expand Down