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

Lines changed: 19 additions & 10 deletions
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

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

guide/angular-data/resource/resource.md

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 19 additions & 10 deletions
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

Lines changed: 7 additions & 3 deletions
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

Lines changed: 9 additions & 8 deletions
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

Lines changed: 6 additions & 3 deletions
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

Lines changed: 7 additions & 3 deletions
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

Lines changed: 6 additions & 3 deletions
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', {

0 commit comments

Comments
 (0)