Skip to content

Commit

Permalink
added support for Backbone.Model for leaf nodes - issue dhruvaray#124
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvaray committed Mar 22, 2014
1 parent 455653b commit 799a224
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 3 additions & 3 deletions backbone-associations.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
var pathTokens = getPathArray(attr), initials = _.initial(pathTokens),
last = pathTokens[pathTokens.length - 1],
parentModel = this.get(initials);
if (parentModel instanceof AssociatedModel) {
if (parentModel instanceof BackboneModel) {
obj = modelMap[parentModel.cid] ||
(modelMap[parentModel.cid] = {'model': parentModel, 'data': {}});
obj.data[last] = attributes[attr];
Expand Down Expand Up @@ -293,7 +293,7 @@

} else if (relation.type === Backbone.One) {

data = val instanceof AssociatedModel ? val : new relatedModel(val, relationOptions);
data = val instanceof BackboneModel ? val : new relatedModel(val, relationOptions);

//Is the passed in data for the same key?
if (currVal && data.attributes.hasOwnProperty(idKey) &&
Expand All @@ -302,7 +302,7 @@
// will not get events until the entire object graph is updated.
currVal._deferEvents = true;
// Perform the traditional `set` operation
currVal._set(val instanceof AssociatedModel ? val.attributes : val, relationOptions);
currVal._set(val instanceof BackboneModel ? val.attributes : val, relationOptions);
data = currVal;
} else {
newCtx = true;
Expand Down
14 changes: 13 additions & 1 deletion test/associated-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1696,7 +1696,7 @@ $(document).ready(function () {

});

test("Issue #124", 4, function () {
test("Issue #124", 7, function () {

Backbone.AssociatedModel = Backbone.Model;
Backbone.Model = Backbone.OriginalModel;
Expand Down Expand Up @@ -1726,6 +1726,18 @@ $(document).ready(function () {
equal(parent.get('myChild') instanceof Backbone.Model, true);
equal(parent.get('myChild') instanceof Backbone.AssociatedModel, false);

parent.on('change:myChild', function () {
ok('came here')
});
parent.on('change:myChild.someValue', function () {
ok('came here')
});

//equivalent to parent.get('myChild').set('someValue',6);
parent.set('myChild.someValue', 6);
equal(parent.get('myChild.someValue'), 6);


});


Expand Down

0 comments on commit 799a224

Please sign in to comment.