Skip to content

Commit 3088f3d

Browse files
committed
Fixes #132 again
1 parent a3438d4 commit 3088f3d

File tree

10 files changed

+88
-48
lines changed

10 files changed

+88
-48
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
##### 1.0.0-beta.2 - xx August 2014
22

3+
###### Breaking API changes
4+
- `findBelongsTo` option of `DS.inject` now defaults to `false`
5+
6+
###### Backwards compatible API changes
7+
- #132 - Added `findHasMany` option and capability to `DS.inject`
8+
39
###### Backwards compatible bug fixes
410
- #135 - loadrelations loop
511

dist/angular-data.js

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2255,7 +2255,7 @@ function create(resourceName, attrs, options) {
22552255
})
22562256
.then(function (data) {
22572257
if (options.cacheResponse) {
2258-
var created = DS.inject(definition.name, data);
2258+
var created = DS.inject(definition.name, data, options);
22592259
var id = created[definition.idAttribute];
22602260
resource.completedQueries[id] = new Date().getTime();
22612261
resource.previousAttributes[id] = DS.utils.deepMixIn({}, created);
@@ -2542,7 +2542,7 @@ function find(resourceName, id, options) {
25422542
// Query is no longer pending
25432543
delete resource.pendingQueries[id];
25442544
resource.completedQueries[id] = new Date().getTime();
2545-
return DS.inject(resourceName, data);
2545+
return DS.inject(resourceName, data, options);
25462546
} else {
25472547
return data;
25482548
}
@@ -2570,7 +2570,7 @@ function errorPrefix(resourceName) {
25702570
return 'DS.findAll(' + resourceName + ', params[, options]): ';
25712571
}
25722572

2573-
function processResults(data, resourceName, queryHash) {
2573+
function processResults(data, resourceName, queryHash, options) {
25742574
var DS = this;
25752575
var resource = DS.store[resourceName];
25762576
var idAttribute = DS.definitions[resourceName].idAttribute;
@@ -2586,7 +2586,7 @@ function processResults(data, resourceName, queryHash) {
25862586
resource.collectionModified = DS.utils.updateTimestamp(resource.collectionModified);
25872587

25882588
// Merge the new values into the cache
2589-
var injected = DS.inject(resourceName, data);
2589+
var injected = DS.inject(resourceName, data, options);
25902590

25912591
// Make sure each object is added to completedQueries
25922592
if (DS.utils.isArray(injected)) {
@@ -2624,7 +2624,7 @@ function _findAll(resourceName, params, options) {
26242624
var data = definition.deserialize(resourceName, res);
26252625
if (options.cacheResponse) {
26262626
try {
2627-
return processResults.apply(DS, [data, resourceName, queryHash]);
2627+
return processResults.apply(DS, [data, resourceName, queryHash, options]);
26282628
} catch (err) {
26292629
return DS.$q.reject(err);
26302630
}
@@ -2929,6 +2929,14 @@ function loadRelations(resourceName, instance, relations, options) {
29292929
throw new IA(errorPrefix(resourceName) + 'options: Must be an object!');
29302930
}
29312931

2932+
if (!('findBelongsTo' in options)) {
2933+
options.findBelongsTo = true;
2934+
}
2935+
2936+
if (!('findHasMany' in options)) {
2937+
options.findHasMany = true;
2938+
}
2939+
29322940
var tasks = [];
29332941
var fields = [];
29342942

@@ -5553,22 +5561,32 @@ function _injectRelations(definition, injected, options) {
55535561
} catch (err) {
55545562
DS.$log.error(errorPrefix(definition.name) + 'Failed to inject ' + type + ' relation: "' + relationName + '"!', err);
55555563
}
5556-
} else if (options.findBelongsTo) {
5557-
if (type === 'belongsTo') {
5558-
if (DS.utils.isArray(injected)) {
5559-
DS.utils.forEach(injected, function (injectedItem) {
5560-
var parent = injectedItem[def.localKey] ? DS.get(relationName, injectedItem[def.localKey]) : null;
5561-
if (parent) {
5562-
injectedItem[def.localField] = parent;
5563-
}
5564-
});
5565-
} else {
5566-
var parent = injected[def.localKey] ? DS.get(relationName, injected[def.localKey]) : null;
5564+
} else if (options.findBelongsTo && type === 'belongsTo') {
5565+
if (DS.utils.isArray(injected)) {
5566+
DS.utils.forEach(injected, function (injectedItem) {
5567+
var parent = injectedItem[def.localKey] ? DS.get(relationName, injectedItem[def.localKey]) : null;
55675568
if (parent) {
5568-
injected[def.localField] = parent;
5569+
injectedItem[def.localField] = parent;
55695570
}
5571+
});
5572+
} else {
5573+
var parent = injected[def.localKey] ? DS.get(relationName, injected[def.localKey]) : null;
5574+
if (parent) {
5575+
injected[def.localField] = parent;
55705576
}
55715577
}
5578+
} else if (options.findHasMany && type === 'hasMany') {
5579+
if (DS.utils.isArray(injected)) {
5580+
DS.utils.forEach(injected, function (injectedItem) {
5581+
var params = {};
5582+
params[def.foreignKey] = injectedItem[def.foreignKey];
5583+
injectedItem[def.localField] = DS.defaults.constructor.prototype.defaultFilter.call(DS, DS.store[relationName].collection, relationName, params, { allowSimpleWhere: true });
5584+
});
5585+
} else {
5586+
var params = {};
5587+
params[def.foreignKey] = injected[def.foreignKey];
5588+
injected[def.localField] = DS.defaults.constructor.prototype.defaultFilter.call(DS, DS.store[relationName].collection, relationName, params, { allowSimpleWhere: true });
5589+
}
55725590
}
55735591
}
55745592

@@ -5632,7 +5650,8 @@ function _injectRelations(definition, injected, options) {
56325650
* @param {object|array} attrs The item or collection of items to inject into the data store.
56335651
* @param {object=} options The item or collection of items to inject into the data store. Properties:
56345652
*
5635-
* - `{boolean=}` - `findBelongsTo` - Find and attach any existing "belongsTo" relationships to the newly injected item. Default: `true`.
5653+
* - `{boolean=}` - `findBelongsTo` - Find and attach any existing "belongsTo" relationships to the newly injected item. Potentially expensive if enabled. Default: `false`.
5654+
* - `{boolean=}` - `findHasMany` - Find and attach any existing "hasMany" relationships to the newly injected item. Potentially expensive if enabled. Default: `false`.
56365655
*
56375656
* @returns {object|array} A reference to the item that was injected into the data store or an array of references to
56385657
* the items that were injected into the data store.
@@ -5654,10 +5673,6 @@ function inject(resourceName, attrs, options) {
56545673
var resource = DS.store[resourceName];
56555674
var injected;
56565675

5657-
if (!('findBelongsTo' in options)) {
5658-
options.findBelongsTo = true;
5659-
}
5660-
56615676
stack++;
56625677

56635678
try {

dist/angular-data.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/datastore/async_methods/create.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function create(resourceName, attrs, options) {
9494
})
9595
.then(function (data) {
9696
if (options.cacheResponse) {
97-
var created = DS.inject(definition.name, data);
97+
var created = DS.inject(definition.name, data, options);
9898
var id = created[definition.idAttribute];
9999
resource.completedQueries[id] = new Date().getTime();
100100
resource.previousAttributes[id] = DS.utils.deepMixIn({}, created);

src/datastore/async_methods/find.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function find(resourceName, id, options) {
8383
// Query is no longer pending
8484
delete resource.pendingQueries[id];
8585
resource.completedQueries[id] = new Date().getTime();
86-
return DS.inject(resourceName, data);
86+
return DS.inject(resourceName, data, options);
8787
} else {
8888
return data;
8989
}

src/datastore/async_methods/findAll.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function errorPrefix(resourceName) {
22
return 'DS.findAll(' + resourceName + ', params[, options]): ';
33
}
44

5-
function processResults(data, resourceName, queryHash) {
5+
function processResults(data, resourceName, queryHash, options) {
66
var DS = this;
77
var resource = DS.store[resourceName];
88
var idAttribute = DS.definitions[resourceName].idAttribute;
@@ -18,7 +18,7 @@ function processResults(data, resourceName, queryHash) {
1818
resource.collectionModified = DS.utils.updateTimestamp(resource.collectionModified);
1919

2020
// Merge the new values into the cache
21-
var injected = DS.inject(resourceName, data);
21+
var injected = DS.inject(resourceName, data, options);
2222

2323
// Make sure each object is added to completedQueries
2424
if (DS.utils.isArray(injected)) {
@@ -56,7 +56,7 @@ function _findAll(resourceName, params, options) {
5656
var data = definition.deserialize(resourceName, res);
5757
if (options.cacheResponse) {
5858
try {
59-
return processResults.apply(DS, [data, resourceName, queryHash]);
59+
return processResults.apply(DS, [data, resourceName, queryHash, options]);
6060
} catch (err) {
6161
return DS.$q.reject(err);
6262
}

src/datastore/async_methods/loadRelations.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ function loadRelations(resourceName, instance, relations, options) {
8484
throw new IA(errorPrefix(resourceName) + 'options: Must be an object!');
8585
}
8686

87+
if (!('findBelongsTo' in options)) {
88+
options.findBelongsTo = true;
89+
}
90+
91+
if (!('findHasMany' in options)) {
92+
options.findHasMany = true;
93+
}
94+
8795
var tasks = [];
8896
var fields = [];
8997

src/datastore/sync_methods/inject.js

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -134,22 +134,32 @@ function _injectRelations(definition, injected, options) {
134134
} catch (err) {
135135
DS.$log.error(errorPrefix(definition.name) + 'Failed to inject ' + type + ' relation: "' + relationName + '"!', err);
136136
}
137-
} else if (options.findBelongsTo) {
138-
if (type === 'belongsTo') {
139-
if (DS.utils.isArray(injected)) {
140-
DS.utils.forEach(injected, function (injectedItem) {
141-
var parent = injectedItem[def.localKey] ? DS.get(relationName, injectedItem[def.localKey]) : null;
142-
if (parent) {
143-
injectedItem[def.localField] = parent;
144-
}
145-
});
146-
} else {
147-
var parent = injected[def.localKey] ? DS.get(relationName, injected[def.localKey]) : null;
137+
} else if (options.findBelongsTo && type === 'belongsTo') {
138+
if (DS.utils.isArray(injected)) {
139+
DS.utils.forEach(injected, function (injectedItem) {
140+
var parent = injectedItem[def.localKey] ? DS.get(relationName, injectedItem[def.localKey]) : null;
148141
if (parent) {
149-
injected[def.localField] = parent;
142+
injectedItem[def.localField] = parent;
150143
}
144+
});
145+
} else {
146+
var parent = injected[def.localKey] ? DS.get(relationName, injected[def.localKey]) : null;
147+
if (parent) {
148+
injected[def.localField] = parent;
151149
}
152150
}
151+
} else if (options.findHasMany && type === 'hasMany') {
152+
if (DS.utils.isArray(injected)) {
153+
DS.utils.forEach(injected, function (injectedItem) {
154+
var params = {};
155+
params[def.foreignKey] = injectedItem[def.foreignKey];
156+
injectedItem[def.localField] = DS.defaults.constructor.prototype.defaultFilter.call(DS, DS.store[relationName].collection, relationName, params, { allowSimpleWhere: true });
157+
});
158+
} else {
159+
var params = {};
160+
params[def.foreignKey] = injected[def.foreignKey];
161+
injected[def.localField] = DS.defaults.constructor.prototype.defaultFilter.call(DS, DS.store[relationName].collection, relationName, params, { allowSimpleWhere: true });
162+
}
153163
}
154164
}
155165

@@ -213,7 +223,8 @@ function _injectRelations(definition, injected, options) {
213223
* @param {object|array} attrs The item or collection of items to inject into the data store.
214224
* @param {object=} options The item or collection of items to inject into the data store. Properties:
215225
*
216-
* - `{boolean=}` - `findBelongsTo` - Find and attach any existing "belongsTo" relationships to the newly injected item. Default: `true`.
226+
* - `{boolean=}` - `findBelongsTo` - Find and attach any existing "belongsTo" relationships to the newly injected item. Potentially expensive if enabled. Default: `false`.
227+
* - `{boolean=}` - `findHasMany` - Find and attach any existing "hasMany" relationships to the newly injected item. Potentially expensive if enabled. Default: `false`.
217228
*
218229
* @returns {object|array} A reference to the item that was injected into the data store or an array of references to
219230
* the items that were injected into the data store.
@@ -235,10 +246,6 @@ function inject(resourceName, attrs, options) {
235246
var resource = DS.store[resourceName];
236247
var injected;
237248

238-
if (!('findBelongsTo' in options)) {
239-
options.findBelongsTo = true;
240-
}
241-
242249
stack++;
243250

244251
try {

test/integration/datastore/async_methods/create.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ describe('DS.create(resourceName, attrs[, options])', function () {
114114
profile: {
115115
email: 'sally@test.com'
116116
}
117+
}, {
118+
findBelongsTo: true
117119
}).then(function (user) {
118120
assert.deepEqual(user.id, payload.id, 'user should have been created');
119121

test/integration/datastore/async_methods/loadRelations.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ describe('DS.loadRelations(resourceName, instance(Id), relations[, options]): ',
7676
$httpBackend.expectGET('http://test.angular-cache.com/user/20').respond(200, user20);
7777
$httpBackend.expectGET('http://test.angular-cache.com/user/19').respond(200, user19);
7878
DS.loadRelations('comment', 19, ['user']).then(function (comment) {
79-
assert.deepEqual(comment.user, user20);
80-
assert.deepEqual(comment.approvedByUser, user19);
79+
assert.isObject(comment.user);
80+
assert.equal(comment.user.id, user20.id);
81+
assert.isObject(comment.approvedByUser);
82+
assert.equal(comment.approvedByUser.id, user19.id);
8183
}, fail);
8284
$httpBackend.flush();
8385
});

0 commit comments

Comments
 (0)