@@ -170,28 +170,6 @@ export default Ember.Object.extend(FetchMixin, Evented, {
170170 } ) ;
171171 } ,
172172
173- /**
174- Patch a relationship, either add or remove, sends a PATCH request
175-
176- Adds with payload: `{ "data": { "type": "comments", "id": "12" } }`
177- Removes with payload: `{ "data": null }` for to-one or `{ "data": [] }` for to-many
178-
179- @method patchRelationship
180- @param {Resource } the resource instance, has URLs via it's relationships property
181- @param {String } resource name (plural) to find the url from the resource instance
182- @return {Promise }
183- */
184- patchRelationship ( resource , relationship ) {
185- let url = resource . get ( [ 'relationships' , relationship , 'links' , 'self' ] . join ( '.' ) ) ;
186- url = url || [ this . get ( 'url' ) , resource . get ( 'id' ) , 'relationships' , relationship ] . join ( '/' ) ;
187- let data = resource . get ( [ 'relationships' , relationship , 'data' ] . join ( '.' ) ) ;
188- data = { data : data } ;
189- return this . fetch ( url , {
190- method : 'PATCH' ,
191- body : JSON . stringify ( data )
192- } ) ;
193- } ,
194-
195173 /**
196174 Delete an existing resource, sends a DELETE request
197175
@@ -211,6 +189,94 @@ export default Ember.Object.extend(FetchMixin, Evented, {
211189 return this . fetch ( url , { method : 'DELETE' } ) ;
212190 } ,
213191
192+ /**
193+ Creates a relationship, sends a POST request
194+
195+ Adds using a payload with the resource object:
196+
197+ - to-one: `{ "data": { "type": "authors", "id": "1" } }`
198+ - to-many: `{ "data": [{ "type": "comments", "id": "12" }] }`
199+
200+ @method createRelationship
201+ @param {Resource } resource instance, has URLs via it's relationships property
202+ @param {String } relationship name (plural) to find the url from the resource instance
203+ @param {String } id of the related resource
204+ @return {Promise }
205+ */
206+ createRelationship ( resource , relationship , id ) {
207+ return this . fetch ( this . _urlForRelationship ( resource , relationship ) , {
208+ method : 'POST' ,
209+ body : JSON . stringify ( this . _payloadForRelationship ( resource , relationship , id ) )
210+ } ) ;
211+ } ,
212+
213+ /**
214+ Patch a relationship, either adds or removes everyting, sends a PATCH request
215+
216+ Adds with payload: `{ "data": { "type": "comments", "id": "12" } }`
217+ Removes with payload: `{ "data": null }` for to-one or `{ "data": [] }` for to-many
218+
219+ @method patchRelationship
220+ @param {Resource } resource instance, has URLs via it's relationships property
221+ @param {String } relationship name (plural) to find the url from the resource instance
222+ @return {Promise }
223+ */
224+ patchRelationship ( resource , relationship ) {
225+ return this . fetch ( this . _urlForRelationship ( resource , relationship ) , {
226+ method : 'PATCH' ,
227+ body : JSON . stringify ( {
228+ data : resource . get ( [ 'relationships' , relationship , 'data' ] . join ( '.' ) )
229+ } )
230+ } ) ;
231+ } ,
232+
233+ /**
234+ Deletes a relationship, sends a DELETE request
235+
236+ Removes using a payload with the resource object:
237+
238+ - to-one: `{ "data": { "type": "authors", "id": "1" } }`
239+ - to-many: `{ "data": [{ "type": "comments", "id": "12" }] }`
240+
241+ @method deleteRelationship
242+ @param {Resource } resource instance, has URLs via it's relationships property
243+ @param {String } relationship name (plural) to find the url from the resource instance
244+ @param {String } id of the related resource
245+ @return {Promise }
246+ */
247+ deleteRelationship ( resource , relationship , id ) {
248+ return this . fetch ( this . _urlForRelationship ( resource , relationship ) , {
249+ method : 'DELETE' ,
250+ body : JSON . stringify ( this . _payloadForRelationship ( resource , relationship , id ) )
251+ } ) ;
252+ } ,
253+
254+ /**
255+ @method _urlForRelationship
256+ @private
257+ @param {Resource } [resource] instance, has URLs via it's relationships property
258+ @param {String } [relationship] name (plural) to find the url from the resource instance
259+ @return {String } url
260+ */
261+ _urlForRelationship ( resource , relationship ) {
262+ let url = resource . get ( [ 'relationships' , relationship , 'links' , 'self' ] . join ( '.' ) ) ;
263+ return url || [ this . get ( 'url' ) , resource . get ( 'id' ) , 'relationships' , relationship ] . join ( '/' ) ;
264+ } ,
265+
266+ /**
267+ @method _payloadForRelationship
268+ @private
269+ @param {Resource } [resource] instance, has URLs via it's relationships property
270+ @param {String } [relationship] name (plural) to find the url from the resource instance
271+ @param {String } [id] the id for the related resource
272+ @return {Object } payload
273+ */
274+ _payloadForRelationship ( resource , relationship , id ) {
275+ let data = resource . get ( [ 'relationships' , relationship , 'data' ] . join ( '.' ) ) ;
276+ let resourceObject = { type : pluralize ( relationship ) , id : id } ;
277+ return { data : ( Array . isArray ( data ) ) ? [ resourceObject ] : resourceObject } ;
278+ } ,
279+
214280 /**
215281 Fetches data using Fetch API or XMLHttpRequest
216282
0 commit comments