Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
allow access to partial values during object graph setup - issue #121
  • Loading branch information
dhruvaray committed Mar 23, 2014
1 parent aeadf1a commit bcb2633
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
13 changes: 11 additions & 2 deletions backbone-associations.js
Expand Up @@ -176,7 +176,9 @@

// Get the value of an attribute.
get:function (attr) {
var obj = ModelProto.get.call(this, attr);
var cache = this.___attributes__,
val = ModelProto.get.call(this, attr),
obj = cache ? val || cache[attr] : val;
return obj ? obj : this._getAttr.apply(this, arguments);
},

Expand All @@ -203,6 +205,10 @@
_set:function (attributes, options) {
var attr, modelMap, modelId, obj, result = this;
if (!attributes) return this;

// temp cache of attributes
this.__attributes__ = attributes;

for (attr in attributes) {
//Create a map for each unique object whose attributes we want to set
modelMap || (modelMap = {});
Expand Down Expand Up @@ -230,6 +236,8 @@
} else {
result = this._setAttr.call(this, attributes, options);
}
delete this.__attributes__;

return result;

},
Expand Down Expand Up @@ -687,6 +695,7 @@
_getAttr:function (path) {

var result = this,
cache = this.__attributes__,
//Tokenize the path
attrs = getPathArray(path),
key,
Expand All @@ -698,7 +707,7 @@
//Navigate the path to get to the result
result = result instanceof BackboneCollection
? (isNaN(key) ? undefined : result.at(key))
: result.attributes[key];
: (cache ? result.attributes[key] || cache[key] : result.attributes[key]);
}
return result;
}
Expand Down
15 changes: 5 additions & 10 deletions test/associated-model.js
Expand Up @@ -1700,20 +1700,15 @@ $(document).ready(function () {

var Designation = Backbone.AssociatedModel.extend({
initialize: function () {
var parent = this.parents[0];
parent.on('change:state', function (model, value, options) {
this.set('name', value == 'NY' ? 'Associate' : 'Dy. Manager')
}, this);
var state = this.parents[0].get('state');
this.set('name', state == 'NY' ? 'Associate' : 'Dy. Manager')
}
});
var Product = Backbone.AssociatedModel.extend({
initialize: function () {
var parent = this.collection.parents[0];
parent.on('change:state', function (model, value, options) {
if (value == 'NY')
this.set('name', this.get('name') + '-' + this.get('calories'));

}, this);
var state = this.collection.parents[0].get('state');
if (state = 'NY')
this.set('name', this.get('name') + '-' + this.get('calories'));
}
});

Expand Down

2 comments on commit bcb2633

@sarink
Copy link

@sarink sarink commented on bcb2633 Mar 23, 2014

Choose a reason for hiding this comment

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

Looks like line 179 references this.___attributes__ (there's an extra underscore) and everywhere else uses this.__attributes__ - is this a typo?

@dhruvaray
Copy link
Owner Author

Choose a reason for hiding this comment

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

Oops... Fixed with the next commit! Thanks!

Please sign in to comment.