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

test case for issue #60 #117

Open
wants to merge 2 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
9 changes: 8 additions & 1 deletion backbone-nested.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@

var fullPathLength = attrPath.length;
var model = this;
var shouldTriggerAdd;

var parentPath = Backbone.NestedModel.createAttrStr(_.initial(attrPath));
if (parentPath && _.isArray(this.get(parentPath)) &&
this.get(Backbone.NestedModel.createAttrStr(attrPath)) === undefined) {
shouldTriggerAdd = true;
}

Backbone.NestedModel.walkPath(newAttrs, attrPath, function(val, path, next){
var attr = _.last(path);
Expand Down Expand Up @@ -314,7 +321,7 @@
model._delayedChange(attrStr, val[attr], opts);
}

if (_.isArray(val[attr])){
if (_.isArray(val[attr]) && shouldTriggerAdd){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about checking isNewValue here instead? Is that sufficient?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah that doesn't work. That variable seems to be for checking if the value has changed, whereas what I'm doing here is checking if the parent is an array, and this array element does not exist yet.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, cool. I'll get this checkout out properly and merged in soon.

model._delayedTrigger('add:' + attrStr, model, val[attr]);
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/nested-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,24 @@ $(document).ready(function() {
deepEqual(doc.get('addresses[0].areaCodes'), []);
});

test("#set() 1-1 inside an array should not trigger the add event", function () {
var entity = new Klass({
names: [
{ first: 'John' }
]
});

var callbackFired = false;

entity.on('add:names', function () {
callbackFired = true;
});

entity.set('names[0].first', 'Steve');

ok(!callbackFired, "Callback should not have been fired");
});


// ----- TO_JSON --------

Expand Down