Skip to content

Commit

Permalink
Check existence, not value of identity
Browse files Browse the repository at this point in the history
In relations with type Backbone.One, _setAttr was checking that data's id
has a truthy value before comparing the identity with currVal's. This
means that if id's value is falsy, such as 0, the predicate fails even if
the actual values were the same. By checking for the id's existence
instead, we match things up correctly even with falsy identities.
  • Loading branch information
arirahikkala committed Feb 25, 2014
1 parent f9aa28c commit 210f188
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion backbone-associations.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@

data = val instanceof AssociatedModel ? val : new relatedModel(val, relationOptions);
//Is the passed in data for the same key?
if (currVal && data.attributes[idKey] &&
if (currVal && data.attributes.hasOwnProperty(idKey) &&
currVal.attributes[idKey] === data.attributes[idKey]) {
// Setting this flag will prevent events from firing immediately. That way clients
// will not get events until the entire object graph is updated.
Expand Down
20 changes: 20 additions & 0 deletions test/associated-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2920,6 +2920,26 @@ $(document).ready(function () {
node1.set({parent:node3, children:[node2, node3]});
});

test("Backbone.One with child having falsy id", 1, function () {
var Child = Backbone.AssociatedModel.extend({
});

var Parent = Backbone.AssociatedModel.extend({
relations: [
{
type: Backbone.One,
key: "child",
relatedModel: Child
}
]
});
var parent = new Parent();
parent.set({child: {id: 0}});
var originalCid = parent.get('child').cid;
parent.set({child: {id: 0}});
ok(originalCid == parent.get('child').cid);
});

test("set,trigger", 13, function () {
node1.on("change:parent", function () {
node1.trigger("nestedevent", arguments);
Expand Down

0 comments on commit 210f188

Please sign in to comment.