Skip to content

Commit d402232

Browse files
committed
Refactored nested endpoints. #40.
1 parent fb7578c commit d402232

File tree

13 files changed

+96
-58
lines changed

13 files changed

+96
-58
lines changed

dist/angular-data.js

+19-10
Original file line numberDiff line numberDiff line change
@@ -4547,20 +4547,29 @@ function defineResource(definition) {
45474547
def.getEndpoint = function (attrs, options) {
45484548
var parent = this.parent;
45494549
var parentKey = this.parentKey;
4550+
var item;
4551+
var endpoint;
45504552
options = options || {};
4551-
if (!('nested' in options)) {
4552-
options.nested = true;
4553-
}
4554-
if (parent && parentKey && definitions[parent] && options.nested) {
4553+
options.params = options.params || {};
4554+
if (parent && parentKey && definitions[parent] && options.params[parentKey] !== false) {
4555+
if (DS.utils.isNumber(attrs) || DS.utils.isString(attrs)) {
4556+
item = DS.get(this.name, attrs);
4557+
}
45554558
if (DS.utils.isObject(attrs) && parentKey in attrs) {
4556-
return DS.utils.makePath(definitions[parent].getEndpoint(attrs, options), attrs[parentKey], this.endpoint);
4557-
} else if ((DS.utils.isNumber(attrs) || DS.utils.isString(attrs)) && DS.get(this.name, attrs) && parentKey in DS.get(this.name, attrs)) {
4558-
return DS.utils.makePath(definitions[parent].getEndpoint(attrs, options), DS.get(this.name, attrs)[parentKey], this.endpoint);
4559-
} else if (options && options.parentKey) {
4560-
return DS.utils.makePath(definitions[parent].getEndpoint(attrs, options), options.parentKey, this.endpoint);
4559+
delete options.params[parentKey];
4560+
endpoint = DS.utils.makePath(definitions[parent].getEndpoint(attrs, options), attrs[parentKey], this.endpoint);
4561+
} else if (item && parentKey in item) {
4562+
delete options.params[parentKey];
4563+
endpoint = DS.utils.makePath(definitions[parent].getEndpoint(attrs, options), item[parentKey], this.endpoint);
4564+
} else if (options && options.params[parentKey]) {
4565+
endpoint = DS.utils.makePath(definitions[parent].getEndpoint(attrs, options), options.params[parentKey], this.endpoint);
4566+
delete options.params[parentKey];
45614567
}
45624568
}
4563-
return this.endpoint;
4569+
if (options.params[parentKey] === false) {
4570+
delete options.params[parentKey];
4571+
}
4572+
return endpoint || this.endpoint;
45644573
};
45654574

45664575
// Remove this in v0.11.0 and make a breaking change notice

dist/angular-data.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

guide/angular-data/resource/resource.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ DS.defineResource({
440440

441441
// The comment isn't in the data store yet, so angular-data wouldn't know
442442
// what the id of the parent "post" would be, so we pass it in manually
443-
DS.find('comment', 5, { parentKey: 4 }); // GET /post/4/comment/5
443+
DS.find('comment', 5, { params: { postId: 4 } }); // GET /post/4/comment/5
444444

445445
// vs
446446

@@ -454,7 +454,7 @@ DS.update('comment', 1, { content: 'stuff' }); // PUT /post/2/comment/1
454454

455455
// If you don't want the nested for just one of the calls then
456456
// you can do the following:
457-
DS.update('comment', 1, { content: 'stuff' }, { nested: false ); // PUT /comment/1
457+
DS.update('comment', 1, { content: 'stuff' }, { params: { postId: false } }); // PUT /comment/1
458458
```
459459
460460
@doc overview

karma.start.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ function startInjector() {
138138
hasMany: {
139139
comment: {
140140
localField: 'comments',
141-
foreignKey: 'userId'
141+
foreignKey: 'approvedBy'
142142
}
143143
},
144144
hasOne: {

src/datastore/sync_methods/defineResource.js

+19-10
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,29 @@ function defineResource(definition) {
147147
def.getEndpoint = function (attrs, options) {
148148
var parent = this.parent;
149149
var parentKey = this.parentKey;
150+
var item;
151+
var endpoint;
150152
options = options || {};
151-
if (!('nested' in options)) {
152-
options.nested = true;
153-
}
154-
if (parent && parentKey && definitions[parent] && options.nested) {
153+
options.params = options.params || {};
154+
if (parent && parentKey && definitions[parent] && options.params[parentKey] !== false) {
155+
if (DS.utils.isNumber(attrs) || DS.utils.isString(attrs)) {
156+
item = DS.get(this.name, attrs);
157+
}
155158
if (DS.utils.isObject(attrs) && parentKey in attrs) {
156-
return DS.utils.makePath(definitions[parent].getEndpoint(attrs, options), attrs[parentKey], this.endpoint);
157-
} else if ((DS.utils.isNumber(attrs) || DS.utils.isString(attrs)) && DS.get(this.name, attrs) && parentKey in DS.get(this.name, attrs)) {
158-
return DS.utils.makePath(definitions[parent].getEndpoint(attrs, options), DS.get(this.name, attrs)[parentKey], this.endpoint);
159-
} else if (options && options.parentKey) {
160-
return DS.utils.makePath(definitions[parent].getEndpoint(attrs, options), options.parentKey, this.endpoint);
159+
delete options.params[parentKey];
160+
endpoint = DS.utils.makePath(definitions[parent].getEndpoint(attrs, options), attrs[parentKey], this.endpoint);
161+
} else if (item && parentKey in item) {
162+
delete options.params[parentKey];
163+
endpoint = DS.utils.makePath(definitions[parent].getEndpoint(attrs, options), item[parentKey], this.endpoint);
164+
} else if (options && options.params[parentKey]) {
165+
endpoint = DS.utils.makePath(definitions[parent].getEndpoint(attrs, options), options.params[parentKey], this.endpoint);
166+
delete options.params[parentKey];
161167
}
162168
}
163-
return this.endpoint;
169+
if (options.params[parentKey] === false) {
170+
delete options.params[parentKey];
171+
}
172+
return endpoint || this.endpoint;
164173
};
165174

166175
// Remove this in v0.11.0 and make a breaking change notice

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ describe('DS.create(resourceName, attrs[, options])', function () {
168168
DS.create('comment', {
169169
content: 'test'
170170
}, {
171-
parentKey: 4
171+
params: {
172+
approvedBy: 4
173+
}
172174
}).then(function (comment) {
173175
assert.deepEqual(comment, testComment2);
174176
assert.deepEqual(comment, DS.get('comment', 6));
@@ -182,9 +184,11 @@ describe('DS.create(resourceName, attrs[, options])', function () {
182184

183185
DS.create('comment', {
184186
content: 'test',
185-
parentKey: 4
187+
approvedBy: 4
186188
}, {
187-
nested: false
189+
params: {
190+
approvedBy: false
191+
}
188192
}).then(function (comment) {
189193
assert.deepEqual(comment, testComment2);
190194
assert.deepEqual(comment, DS.get('comment', 6));

test/integration/datastore/async_methods/destroy.test.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ describe('DS.destroy(resourceName, id)', function () {
5858
$httpBackend.expectDELETE('http://test.angular-cache.com/user/4/comment/5').respond(204);
5959

6060
DS.destroy('comment', 5, {
61-
parentKey: 4
61+
params: {
62+
approvedBy: 4
63+
}
6264
}).then(function () {
63-
}, function (err) {
64-
console.log(err);
65+
}, function () {
6566
fail('Should not have failed!');
6667
});
6768

@@ -74,8 +75,7 @@ describe('DS.destroy(resourceName, id)', function () {
7475
DS.destroy('comment', 6, {
7576
bypassCache: true
7677
}).then(function () {
77-
}, function (err) {
78-
console.log(err);
78+
}, function () {
7979
fail('Should not have failed!');
8080
});
8181

@@ -86,10 +86,11 @@ describe('DS.destroy(resourceName, id)', function () {
8686
DS.inject('comment', testComment2);
8787

8888
DS.destroy('comment', 6, {
89-
nested: false
89+
params: {
90+
approvedBy: false
91+
}
9092
}).then(function () {
91-
}, function (err) {
92-
console.log(err);
93+
}, function () {
9394
fail('Should not have failed!');
9495
});
9596

test/integration/datastore/async_methods/destroyAll.test.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ describe('DS.destroyAll(resourceName, params[, options]): ', function () {
8282
DS.destroyAll('comment', {
8383
content: 'test'
8484
}, {
85-
parentKey: 4
85+
params: {
86+
approvedBy: 4
87+
}
8688
}).then(function () {
8789
}, function (err) {
8890
console.log(err);
@@ -108,8 +110,9 @@ describe('DS.destroyAll(resourceName, params[, options]): ', function () {
108110
DS.destroyAll('comment', {
109111
content: 'test'
110112
}, {
111-
parentKey: 4,
112-
nested: false
113+
params: {
114+
approvedBy: false
115+
}
113116
}).then(function () {
114117
}, function (err) {
115118
console.log(err);

test/integration/datastore/async_methods/find.test.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ describe('DS.find(resourceName, id[, options]): ', function () {
122122
$httpBackend.expectGET('http://test.angular-cache.com/user/4/comment/5').respond(200, testComment);
123123

124124
DS.find('comment', 5, {
125-
parentKey: 4
125+
params: {
126+
approvedBy: 4
127+
}
126128
}).then(function (comment) {
127129
assert.deepEqual(comment, testComment);
128130
assert.deepEqual(comment, DS.get('comment', 5));
@@ -148,8 +150,10 @@ describe('DS.find(resourceName, id[, options]): ', function () {
148150
$httpBackend.expectGET('http://test.angular-cache.com/comment/5').respond(200, testComment);
149151

150152
DS.find('comment', 5, {
151-
nested: false,
152-
bypassCache: true
153+
bypassCache: true,
154+
params: {
155+
approvedBy: false
156+
}
153157
}).then(function (comment) {
154158
assert.deepEqual(comment, testComment);
155159
assert.deepEqual(comment, DS.get('comment', 5));

test/integration/datastore/async_methods/findAll.test.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ describe('DS.findAll(resourceName, params[, options]): ', function () {
250250
DS.findAll('comment', {
251251
content: 'test'
252252
}, {
253-
parentKey: 4
253+
params: {
254+
approvedBy: 4
255+
}
254256
}).then(function (comments) {
255257
assert.deepEqual(comments, [testComment, testComment2]);
256258
assert.deepEqual(comments, DS.filter('comment', {
@@ -288,9 +290,10 @@ describe('DS.findAll(resourceName, params[, options]): ', function () {
288290
DS.findAll('comment', {
289291
content: 'test'
290292
}, {
291-
parentKey: 4,
292293
bypassCache: true,
293-
nested: false
294+
params: {
295+
approvedBy: false
296+
}
294297
}).then(function (comments) {
295298
assert.deepEqual(comments, [testComment, testComment2]);
296299
assert.deepEqual(comments, DS.filter('comment', {

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

+7-8
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ describe('DS.loadRelations(resourceName, instance(Id), relations[, options]): ',
5050
it('should get an item from the server', function () {
5151
DS.inject('user', user10);
5252

53-
$httpBackend.expectGET('http://test.angular-cache.com/organization/14').respond(200, organization14);
54-
$httpBackend.expectGET('http://test.angular-cache.com/comment?userId=10').respond(200, [
53+
$httpBackend.expectGET('http://test.angular-cache.com/organization/14?userId=10').respond(200, organization14);
54+
$httpBackend.expectGET('http://test.angular-cache.com/user/10/comment?userId=10').respond(200, [
5555
comment11,
5656
comment12,
5757
comment13
5858
]);
5959
$httpBackend.expectGET('http://test.angular-cache.com/profile?userId=10').respond(200, profile15);
6060

61-
DS.loadRelations('user', 10, ['comment', 'profile', 'organization']).then(function (user) {
61+
DS.loadRelations('user', 10, ['comment', 'profile', 'organization'], { params: { approvedBy: 10 } }).then(function (user) {
6262
assert.deepEqual(user.comments, [
6363
comment11,
6464
comment12,
@@ -79,7 +79,6 @@ describe('DS.loadRelations(resourceName, instance(Id), relations[, options]): ',
7979
assert.deepEqual(comment.approvedByUser, user19);
8080
}, fail);
8181
$httpBackend.flush();
82-
8382
});
8483
it('should get an item from the server but not store it if cacheResponse is false', function () {
8584
DS.inject('user', {
@@ -88,8 +87,8 @@ describe('DS.loadRelations(resourceName, instance(Id), relations[, options]): ',
8887
organizationId: 14
8988
});
9089

91-
$httpBackend.expectGET('http://test.angular-cache.com/organization/14').respond(200, organization14);
92-
$httpBackend.expectGET('http://test.angular-cache.com/comment?userId=10').respond(200, [
90+
$httpBackend.expectGET('http://test.angular-cache.com/organization/14?userId=10').respond(200, organization14);
91+
$httpBackend.expectGET('http://test.angular-cache.com/user/10/comment?userId=10').respond(200, [
9392
comment11,
9493
comment12,
9594
comment13
@@ -121,8 +120,8 @@ describe('DS.loadRelations(resourceName, instance(Id), relations[, options]): ',
121120
organizationId: 14
122121
});
123122

124-
$httpBackend.expectGET('http://test.angular-cache.com/organization/14').respond(404, 'Not Found');
125-
$httpBackend.expectGET('http://test.angular-cache.com/comment?userId=10').respond(404, 'Not Found');
123+
$httpBackend.expectGET('http://test.angular-cache.com/organization/14?userId=10').respond(404, 'Not Found');
124+
$httpBackend.expectGET('http://test.angular-cache.com/user/10/comment?userId=10').respond(404, 'Not Found');
126125
$httpBackend.expectGET('http://test.angular-cache.com/profile?userId=10').respond(404, 'Not Found');
127126

128127
DS.loadRelations('user', 10, ['comment', 'profile', 'organization']).then(function () {

test/integration/datastore/async_methods/update.test.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ describe('DS.update(resourceName, id, attrs[, options])', function () {
122122
DS.update('comment', 6, {
123123
content: 'stuff'
124124
}, {
125-
parentKey: 4
125+
params: {
126+
approvedBy: 4
127+
}
126128
}).then(function (comment) {
127129
assert.deepEqual(comment, testComment2);
128130
assert.deepEqual(comment, DS.get('comment', 6));
@@ -139,8 +141,9 @@ describe('DS.update(resourceName, id, attrs[, options])', function () {
139141
DS.update('comment', 6, {
140142
content: 'stuff'
141143
}, {
142-
parentKey: 4,
143-
nested: false
144+
params: {
145+
approvedBy: false
146+
}
144147
}).then(function (comment) {
145148
assert.deepEqual(comment, testComment2);
146149
assert.deepEqual(comment, DS.get('comment', 6));

test/integration/datastore/async_methods/updateAll.test.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ describe('DS.updateAll(resourceName, attrs, params[, options])', function () {
116116
}, {
117117
content: 'test'
118118
}, {
119-
parentKey: 4
119+
params: {
120+
approvedBy: 4
121+
}
120122
}).then(function (comments) {
121123
assert.deepEqual(comments, [testComment, testComment2]);
122124
assert.deepEqual(comments, DS.filter('comment', {
@@ -161,8 +163,9 @@ describe('DS.updateAll(resourceName, attrs, params[, options])', function () {
161163
}, {
162164
content: 'test'
163165
}, {
164-
parentKey: 4,
165-
nested: false
166+
params: {
167+
approvedBy: false
168+
}
166169
}).then(function (comments) {
167170
assert.deepEqual(comments, [testComment, testComment2]);
168171
assert.deepEqual(comments, DS.filter('comment', {

0 commit comments

Comments
 (0)