11/**
22* @author Jason Dobry <jason.dobry@gmail.com>
33* @file angular-data.js
4- * @version 1.4.3 - Homepage <http://angular-data.pseudobry.com/>
4+ * @version 1.5.0 - Homepage <http://angular-data.pseudobry.com/>
55* @copyright (c) 2014 Jason Dobry <https://github.com/jmdobry/>
66* @license MIT <https://github.com/jmdobry/angular-data/blob/master/LICENSE>
77*
@@ -1891,6 +1891,36 @@ function DSHttpAdapterProvider() {
18911891
18921892 this . $get = [ '$http' , '$log' , 'DSUtils' , function ( $http , $log , DSUtils ) {
18931893
1894+ /**
1895+ * @doc method
1896+ * @id DSHttpAdapter.methods:getPath
1897+ * @name getPath
1898+ * @description
1899+ * Return the path that would be used by this adapter for a given operation.
1900+ *
1901+ * ## Signature:
1902+ * ```js
1903+ * DSHttpAdapter.getPath(method, resourceConfig, id|attrs|params, options))
1904+ * ```
1905+ *
1906+ * @param {string } method The name of the method .
1907+ * @param {object } resourceConfig The object returned by DS.defineResource.
1908+ * @param {string|object } id|attrs|params The id, attrs, or params that you would pass into the method.
1909+ * @param {object } options Configuration options.
1910+ * @returns {string } The path.
1911+ */
1912+ function getPath ( method , resourceConfig , id , options ) {
1913+ options = options || { } ;
1914+ var args = [
1915+ options . baseUrl || resourceConfig . baseUrl ,
1916+ resourceConfig . getEndpoint ( ( DSUtils . isString ( id ) || DSUtils . isNumber ( id ) || method === 'create' ) ? id : null , options )
1917+ ] ;
1918+ if ( method === 'find' || method === 'update' || method === 'destroy' ) {
1919+ args . push ( id ) ;
1920+ }
1921+ return DSUtils . makePath . apply ( DSUtils , args ) ;
1922+ }
1923+
18941924 /**
18951925 * @doc interface
18961926 * @id DSHttpAdapter
@@ -1909,6 +1939,8 @@ function DSHttpAdapterProvider() {
19091939 */
19101940 defaults : defaults ,
19111941
1942+ getPath : getPath ,
1943+
19121944 /**
19131945 * @doc method
19141946 * @id DSHttpAdapter.methods:HTTP
@@ -2072,7 +2104,7 @@ function DSHttpAdapterProvider() {
20722104 find : function ( resourceConfig , id , options ) {
20732105 options = options || { } ;
20742106 return this . GET (
2075- DSUtils . makePath ( options . baseUrl || resourceConfig . baseUrl , resourceConfig . getEndpoint ( id , options ) , id ) ,
2107+ getPath ( 'find' , resourceConfig , id , options ) ,
20762108 options
20772109 ) ;
20782110 } ,
@@ -2109,7 +2141,7 @@ function DSHttpAdapterProvider() {
21092141 DSUtils . deepMixIn ( options . params , params ) ;
21102142 }
21112143 return this . GET (
2112- DSUtils . makePath ( options . baseUrl || resourceConfig . baseUrl , resourceConfig . getEndpoint ( null , options ) ) ,
2144+ getPath ( 'findAll' , resourceConfig , params , options ) ,
21132145 options
21142146 ) ;
21152147 } ,
@@ -2141,7 +2173,7 @@ function DSHttpAdapterProvider() {
21412173 create : function ( resourceConfig , attrs , options ) {
21422174 options = options || { } ;
21432175 return this . POST (
2144- DSUtils . makePath ( options . baseUrl || resourceConfig . baseUrl , resourceConfig . getEndpoint ( attrs , options ) ) ,
2176+ getPath ( 'create' , resourceConfig , attrs , options ) ,
21452177 attrs ,
21462178 options
21472179 ) ;
@@ -2175,7 +2207,7 @@ function DSHttpAdapterProvider() {
21752207 update : function ( resourceConfig , id , attrs , options ) {
21762208 options = options || { } ;
21772209 return this . PUT (
2178- DSUtils . makePath ( options . baseUrl || resourceConfig . baseUrl , resourceConfig . getEndpoint ( id , options ) , id ) ,
2210+ getPath ( 'update' , resourceConfig , id , options ) ,
21792211 attrs ,
21802212 options
21812213 ) ;
@@ -2214,7 +2246,7 @@ function DSHttpAdapterProvider() {
22142246 DSUtils . deepMixIn ( options . params , params ) ;
22152247 }
22162248 return this . PUT (
2217- DSUtils . makePath ( options . baseUrl || resourceConfig . baseUrl , resourceConfig . getEndpoint ( null , options ) ) ,
2249+ getPath ( 'updateAll' , resourceConfig , attrs , options ) ,
22182250 attrs ,
22192251 options
22202252 ) ;
@@ -2247,7 +2279,7 @@ function DSHttpAdapterProvider() {
22472279 destroy : function ( resourceConfig , id , options ) {
22482280 options = options || { } ;
22492281 return this . DEL (
2250- DSUtils . makePath ( options . baseUrl || resourceConfig . baseUrl , resourceConfig . getEndpoint ( id , options ) , id ) ,
2282+ getPath ( 'destroy' , resourceConfig , id , options ) ,
22512283 options
22522284 ) ;
22532285 } ,
@@ -2284,7 +2316,7 @@ function DSHttpAdapterProvider() {
22842316 DSUtils . deepMixIn ( options . params , params ) ;
22852317 }
22862318 return this . DEL (
2287- DSUtils . makePath ( options . baseUrl || resourceConfig . baseUrl , resourceConfig . getEndpoint ( null , options ) ) ,
2319+ getPath ( 'destroyAll' , resourceConfig , params , options ) ,
22882320 options
22892321 ) ;
22902322 }
@@ -2304,6 +2336,36 @@ function DSLocalStorageAdapterProvider() {
23042336
23052337 this . $get = [ '$q' , 'DSUtils' , 'DSErrors' , function ( $q , DSUtils ) {
23062338
2339+ /**
2340+ * @doc method
2341+ * @id DSLocalStorageAdapter.methods:getPath
2342+ * @name getPath
2343+ * @description
2344+ * Return the path that would be used by this adapter for a given operation.
2345+ *
2346+ * ## Signature:
2347+ * ```js
2348+ * DSLocalStorageAdapter.getPath(method, resourceConfig, id|attrs|params, options))
2349+ * ```
2350+ *
2351+ * @param {string } method The name of the method .
2352+ * @param {object } resourceConfig The object returned by DS.defineResource.
2353+ * @param {string|object } id|attrs|params The id, attrs, or params that you would pass into the method.
2354+ * @param {object } options Configuration options.
2355+ * @returns {string } The path.
2356+ */
2357+ function getPath ( method , resourceConfig , id , options ) {
2358+ options = options || { } ;
2359+ var args = [
2360+ options . baseUrl || resourceConfig . baseUrl ,
2361+ resourceConfig . getEndpoint ( ( DSUtils . isString ( id ) || DSUtils . isNumber ( id ) || method === 'create' ) ? id : null , options )
2362+ ] ;
2363+ if ( method === 'find' || method === 'update' || method === 'destroy' ) {
2364+ args . push ( id ) ;
2365+ }
2366+ return DSUtils . makePath . apply ( DSUtils , args ) ;
2367+ }
2368+
23072369 /**
23082370 * @doc interface
23092371 * @id DSLocalStorageAdapter
@@ -2454,7 +2516,7 @@ function DSLocalStorageAdapterProvider() {
24542516 */
24552517 find : function find ( resourceConfig , id , options ) {
24562518 options = options || { } ;
2457- return this . GET ( DSUtils . makePath ( options . baseUrl || resourceConfig . baseUrl , resourceConfig . endpoint , id ) ) . then ( function ( item ) {
2519+ return this . GET ( getPath ( 'find' , resourceConfig , id , options ) ) . then ( function ( item ) {
24582520 if ( ! item ) {
24592521 return $q . reject ( new Error ( 'Not Found!' ) ) ;
24602522 } else {
@@ -2493,7 +2555,7 @@ function DSLocalStorageAdapterProvider() {
24932555 var items = [ ] ;
24942556 var ids = DSUtils . keys ( _this . getIds ( resourceConfig . name , options ) ) ;
24952557 DSUtils . forEach ( ids , function ( id ) {
2496- var itemJson = localStorage . getItem ( DSUtils . makePath ( options . baseUrl || resourceConfig . baseUrl , resourceConfig . getEndpoint ( id , options ) , id ) ) ;
2558+ var itemJson = localStorage . getItem ( getPath ( 'find' , resourceConfig , id , options ) ) ;
24972559 if ( itemJson ) {
24982560 items . push ( DSUtils . fromJson ( itemJson ) ) ;
24992561 }
@@ -2536,15 +2598,19 @@ function DSLocalStorageAdapterProvider() {
25362598 */
25372599 create : function ( resourceConfig , attrs , options ) {
25382600 var _this = this ;
2539- attrs [ resourceConfig . idAttribute ] = attrs [ resourceConfig . idAttribute ] || DSUtils . guid ( ) ;
2601+ var id = attrs [ resourceConfig . idAttribute ] ;
25402602 options = options || { } ;
2541- return this . PUT (
2542- DSUtils . makePath ( options . baseUrl || resourceConfig . baseUrl , resourceConfig . getEndpoint ( attrs , options ) , attrs [ resourceConfig . idAttribute ] ) ,
2543- attrs
2544- ) . then ( function ( item ) {
2545- _this . ensureId ( item [ resourceConfig . idAttribute ] , resourceConfig . name , options ) ;
2546- return item ;
2547- } ) ;
2603+ return _this . GET ( getPath ( 'find' , resourceConfig , id , options ) ) . then ( function ( item ) {
2604+ if ( item ) {
2605+ DSUtils . deepMixIn ( item , attrs ) ;
2606+ } else {
2607+ attrs [ resourceConfig . idAttribute ] = id = id || DSUtils . guid ( ) ;
2608+ }
2609+ return _this . PUT ( getPath ( 'update' , resourceConfig , id , options ) , item || attrs ) ;
2610+ } ) . then ( function ( item ) {
2611+ _this . ensureId ( item [ resourceConfig . idAttribute ] , resourceConfig . name , options ) ;
2612+ return item ;
2613+ } ) ;
25482614 } ,
25492615
25502616 /**
@@ -2582,9 +2648,13 @@ function DSLocalStorageAdapterProvider() {
25822648 update : function ( resourceConfig , id , attrs , options ) {
25832649 options = options || { } ;
25842650 var _this = this ;
2585- return _this . find ( resourceConfig , id , options ) . then ( function ( item ) {
2651+ return _this . GET ( getPath ( 'find' , resourceConfig , id , options ) ) . then ( function ( item ) {
2652+ item = item || { } ;
25862653 DSUtils . deepMixIn ( item , attrs ) ;
2587- return _this . PUT ( DSUtils . makePath ( options . baseUrl || resourceConfig . baseUrl , resourceConfig . getEndpoint ( id , options ) , id ) , item ) ;
2654+ return _this . PUT ( getPath ( 'update' , resourceConfig , id , options ) , item ) ;
2655+ } ) . then ( function ( item ) {
2656+ _this . ensureId ( item [ resourceConfig . idAttribute ] , resourceConfig . name , options ) ;
2657+ return item ;
25882658 } ) ;
25892659 } ,
25902660
@@ -2653,7 +2723,7 @@ function DSLocalStorageAdapterProvider() {
26532723 */
26542724 destroy : function ( resourceConfig , id , options ) {
26552725 options = options || { } ;
2656- return this . DEL ( DSUtils . makePath ( options . baseUrl || resourceConfig . baseUrl , resourceConfig . getEndpoint ( id , options ) , id ) ) ;
2726+ return this . DEL ( getPath ( 'destroy' , resourceConfig , id , options ) ) ;
26572727 } ,
26582728
26592729 /**
@@ -6814,7 +6884,6 @@ function _link(definition, injected, options) {
68146884 * the items that were injected into the data store.
68156885 */
68166886function inject ( resourceName , attrs , options ) {
6817- console . log ( 'inject' , resourceName , attrs ) ;
68186887 var DS = this ;
68196888 var IA = DS . errors . IA ;
68206889 var definition = DS . definitions [ resourceName ] ;
@@ -6847,9 +6916,7 @@ function inject(resourceName, attrs, options) {
68476916 resource . collectionModified = DS . utils . updateTimestamp ( resource . collectionModified ) ;
68486917 }
68496918
6850- console . log ( options ) ;
68516919 if ( options . linkInverse && typeof options . linkInverse === 'boolean' ) {
6852- console . log ( 'linkInverse' , typeof options . linkInverse , options . linkInverse ) ;
68536920 if ( DS . utils . isArray ( injected ) ) {
68546921 if ( injected . length ) {
68556922 DS . linkInverse ( definition . name , injected [ 0 ] [ definition . idAttribute ] ) ;
@@ -6859,8 +6926,6 @@ function inject(resourceName, attrs, options) {
68596926 }
68606927 }
68616928
6862- console . log ( injected ) ;
6863-
68646929 if ( DS . utils . isArray ( injected ) ) {
68656930 DS . utils . forEach ( injected , function ( injectedI ) {
68666931 _link . call ( DS , definition , injectedI , options ) ;
@@ -7072,7 +7137,6 @@ function _link(definition, linked, relations) {
70727137 * @returns {object|array } A reference to the item with its linked relations.
70737138 */
70747139function link ( resourceName , id , relations ) {
7075- console . log ( 'link' , resourceName , id ) ;
70767140 var DS = this ;
70777141 var IA = DS . errors . IA ;
70787142 var definition = DS . definitions [ resourceName ] ;
@@ -7099,8 +7163,6 @@ function link(resourceName, id, relations) {
70997163 }
71007164 }
71017165
7102- console . log ( 'linked' , linked ) ;
7103-
71047166 return linked ;
71057167}
71067168
0 commit comments