Skip to content

Commit 33d4d55

Browse files
committed
revisit fix for issue dhruvaray#133
1 parent 8b4a41f commit 33d4d55

File tree

2 files changed

+79
-57
lines changed

2 files changed

+79
-57
lines changed

backbone-associations.js

+56-57
Original file line numberDiff line numberDiff line change
@@ -277,73 +277,72 @@
277277

278278
// Map `val` if a transformation function is provided.
279279
val = map ? map.call(this, val, collectionType ? collectionType : relatedModel) : val;
280-
if(!val) {
281-
attributes[relationKey] = val;
282-
return;
283-
}
284-
285-
// If `relation.type` is `Backbone.Many`,
286-
// Create `Backbone.Collection` with passed data and perform Backbone `set`.
287-
if (relation.type === Backbone.Many) {
280+
if (!isValuePresent(val)) {
281+
data = val;
282+
} else {
283+
// If `relation.type` is `Backbone.Many`,
284+
// Create `Backbone.Collection` with passed data and perform Backbone `set`.
285+
if (relation.type === Backbone.Many) {
288286

289-
if (currVal) {
290-
// Setting this flag will prevent events from firing immediately. That way clients
291-
// will not get events until the entire object graph is updated.
292-
currVal._deferEvents = true;
287+
if (currVal) {
288+
// Setting this flag will prevent events from firing immediately. That way clients
289+
// will not get events until the entire object graph is updated.
290+
currVal._deferEvents = true;
293291

294-
// Use Backbone.Collection's `reset` or smart `set` method
295-
currVal[relationOptions.reset ? 'reset' : 'set'](
296-
val instanceof BackboneCollection ? val.models : val, relationOptions);
292+
// Use Backbone.Collection's `reset` or smart `set` method
293+
currVal[relationOptions.reset ? 'reset' : 'set'](
294+
val instanceof BackboneCollection ? val.models : val, relationOptions);
297295

298-
data = currVal;
296+
data = currVal;
299297

300-
} else {
301-
newCtx = true;
302-
303-
if (val instanceof BackboneCollection) {
304-
data = val;
305298
} else {
306-
data = this._createCollection(
307-
collectionType || BackboneCollection,
308-
relation.collectionOptions || (relatedModel ? {model: relatedModel} : {})
309-
);
310-
data[relationOptions.reset ? 'reset' : 'set'](val, relationOptions);
299+
newCtx = true;
300+
301+
if (val instanceof BackboneCollection) {
302+
data = val;
303+
} else {
304+
data = this._createCollection(
305+
collectionType || BackboneCollection,
306+
relation.collectionOptions || (relatedModel ? {model: relatedModel} : {})
307+
);
308+
data[relationOptions.reset ? 'reset' : 'set'](val, relationOptions);
309+
}
311310
}
312-
}
313311

314-
} else if (relation.type === Backbone.One) {
315-
316-
var hasOwnProperty = (val instanceof BackboneModel) ?
317-
val.attributes.hasOwnProperty(idKey) :
318-
val.hasOwnProperty(idKey);
319-
var newIdKey = (val instanceof BackboneModel) ?
320-
val.attributes[idKey] :
321-
val[idKey];
322-
323-
//Is the passed in data for the same key?
324-
if (currVal && hasOwnProperty &&
325-
currVal.attributes[idKey] === newIdKey) {
326-
// Setting this flag will prevent events from firing immediately. That way clients
327-
// will not get events until the entire object graph is updated.
328-
currVal._deferEvents = true;
329-
// Perform the traditional `set` operation
330-
currVal._set(val instanceof BackboneModel ? val.attributes : val, relationOptions);
331-
data = currVal;
332-
} else {
333-
newCtx = true;
334-
335-
if (val instanceof BackboneModel) {
336-
data = val;
312+
} else if (relation.type === Backbone.One) {
313+
314+
var hasOwnProperty = (val instanceof BackboneModel) ?
315+
val.attributes.hasOwnProperty(idKey) :
316+
val.hasOwnProperty(idKey);
317+
var newIdKey = (val instanceof BackboneModel) ?
318+
val.attributes[idKey] :
319+
val[idKey];
320+
321+
//Is the passed in data for the same key?
322+
if (currVal && hasOwnProperty &&
323+
currVal.attributes[idKey] === newIdKey) {
324+
// Setting this flag will prevent events from firing immediately. That way clients
325+
// will not get events until the entire object graph is updated.
326+
currVal._deferEvents = true;
327+
// Perform the traditional `set` operation
328+
currVal._set(val instanceof BackboneModel ? val.attributes : val, relationOptions);
329+
data = currVal;
337330
} else {
338-
relationOptions.__parents__ = this;
339-
data = new relatedModel(val, relationOptions);
340-
delete relationOptions.__parents__;
341-
}
331+
newCtx = true;
332+
333+
if (val instanceof BackboneModel) {
334+
data = val;
335+
} else {
336+
relationOptions.__parents__ = this;
337+
data = new relatedModel(val, relationOptions);
338+
delete relationOptions.__parents__;
339+
}
342340

341+
}
342+
} else {
343+
throw new Error('type attribute must be specified and ' +
344+
'have the values Backbone.One or Backbone.Many');
343345
}
344-
} else {
345-
throw new Error('type attribute must be specified and ' +
346-
'have the values Backbone.One or Backbone.Many');
347346
}
348347

349348

test/associated-model.js

+23
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,29 @@ $(document).ready(function () {
16961696

16971697
});
16981698

1699+
test("Issue #133", 1, function () {
1700+
1701+
var Foo = Backbone.AssociatedModel.extend({});
1702+
1703+
var Bar = Backbone.AssociatedModel.extend({
1704+
relations: [
1705+
{
1706+
type: Backbone.One,
1707+
key: 'rel',
1708+
relatedModel: Foo,
1709+
map: function (m) {
1710+
if (m.test == '') return null;
1711+
return m;
1712+
}
1713+
}
1714+
]
1715+
});
1716+
1717+
var bar = new Bar({rel: {'test': ''}});
1718+
ok('came here');
1719+
1720+
});
1721+
16991722
test("Issue #121 - Traverse child upwards", 2, function () {
17001723

17011724
var Designation = Backbone.AssociatedModel.extend({

0 commit comments

Comments
 (0)