1
1
/**
2
2
* @author Jason Dobry <[email protected] >
3
3
* @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/>
5
5
* @copyright (c) 2014 Jason Dobry <https://github.com/jmdobry/>
6
6
* @license MIT <https://github.com/jmdobry/angular-data/blob/master/LICENSE>
7
7
*
@@ -1891,6 +1891,36 @@ function DSHttpAdapterProvider() {
1891
1891
1892
1892
this . $get = [ '$http' , '$log' , 'DSUtils' , function ( $http , $log , DSUtils ) {
1893
1893
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
+
1894
1924
/**
1895
1925
* @doc interface
1896
1926
* @id DSHttpAdapter
@@ -1909,6 +1939,8 @@ function DSHttpAdapterProvider() {
1909
1939
*/
1910
1940
defaults : defaults ,
1911
1941
1942
+ getPath : getPath ,
1943
+
1912
1944
/**
1913
1945
* @doc method
1914
1946
* @id DSHttpAdapter.methods:HTTP
@@ -2072,7 +2104,7 @@ function DSHttpAdapterProvider() {
2072
2104
find : function ( resourceConfig , id , options ) {
2073
2105
options = options || { } ;
2074
2106
return this . GET (
2075
- DSUtils . makePath ( options . baseUrl || resourceConfig . baseUrl , resourceConfig . getEndpoint ( id , options ) , id ) ,
2107
+ getPath ( 'find' , resourceConfig , id , options ) ,
2076
2108
options
2077
2109
) ;
2078
2110
} ,
@@ -2109,7 +2141,7 @@ function DSHttpAdapterProvider() {
2109
2141
DSUtils . deepMixIn ( options . params , params ) ;
2110
2142
}
2111
2143
return this . GET (
2112
- DSUtils . makePath ( options . baseUrl || resourceConfig . baseUrl , resourceConfig . getEndpoint ( null , options ) ) ,
2144
+ getPath ( 'findAll' , resourceConfig , params , options ) ,
2113
2145
options
2114
2146
) ;
2115
2147
} ,
@@ -2141,7 +2173,7 @@ function DSHttpAdapterProvider() {
2141
2173
create : function ( resourceConfig , attrs , options ) {
2142
2174
options = options || { } ;
2143
2175
return this . POST (
2144
- DSUtils . makePath ( options . baseUrl || resourceConfig . baseUrl , resourceConfig . getEndpoint ( attrs , options ) ) ,
2176
+ getPath ( 'create' , resourceConfig , attrs , options ) ,
2145
2177
attrs ,
2146
2178
options
2147
2179
) ;
@@ -2175,7 +2207,7 @@ function DSHttpAdapterProvider() {
2175
2207
update : function ( resourceConfig , id , attrs , options ) {
2176
2208
options = options || { } ;
2177
2209
return this . PUT (
2178
- DSUtils . makePath ( options . baseUrl || resourceConfig . baseUrl , resourceConfig . getEndpoint ( id , options ) , id ) ,
2210
+ getPath ( 'update' , resourceConfig , id , options ) ,
2179
2211
attrs ,
2180
2212
options
2181
2213
) ;
@@ -2214,7 +2246,7 @@ function DSHttpAdapterProvider() {
2214
2246
DSUtils . deepMixIn ( options . params , params ) ;
2215
2247
}
2216
2248
return this . PUT (
2217
- DSUtils . makePath ( options . baseUrl || resourceConfig . baseUrl , resourceConfig . getEndpoint ( null , options ) ) ,
2249
+ getPath ( 'updateAll' , resourceConfig , attrs , options ) ,
2218
2250
attrs ,
2219
2251
options
2220
2252
) ;
@@ -2247,7 +2279,7 @@ function DSHttpAdapterProvider() {
2247
2279
destroy : function ( resourceConfig , id , options ) {
2248
2280
options = options || { } ;
2249
2281
return this . DEL (
2250
- DSUtils . makePath ( options . baseUrl || resourceConfig . baseUrl , resourceConfig . getEndpoint ( id , options ) , id ) ,
2282
+ getPath ( 'destroy' , resourceConfig , id , options ) ,
2251
2283
options
2252
2284
) ;
2253
2285
} ,
@@ -2284,7 +2316,7 @@ function DSHttpAdapterProvider() {
2284
2316
DSUtils . deepMixIn ( options . params , params ) ;
2285
2317
}
2286
2318
return this . DEL (
2287
- DSUtils . makePath ( options . baseUrl || resourceConfig . baseUrl , resourceConfig . getEndpoint ( null , options ) ) ,
2319
+ getPath ( 'destroyAll' , resourceConfig , params , options ) ,
2288
2320
options
2289
2321
) ;
2290
2322
}
@@ -2304,6 +2336,36 @@ function DSLocalStorageAdapterProvider() {
2304
2336
2305
2337
this . $get = [ '$q' , 'DSUtils' , 'DSErrors' , function ( $q , DSUtils ) {
2306
2338
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
+
2307
2369
/**
2308
2370
* @doc interface
2309
2371
* @id DSLocalStorageAdapter
@@ -2454,7 +2516,7 @@ function DSLocalStorageAdapterProvider() {
2454
2516
*/
2455
2517
find : function find ( resourceConfig , id , options ) {
2456
2518
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 ) {
2458
2520
if ( ! item ) {
2459
2521
return $q . reject ( new Error ( 'Not Found!' ) ) ;
2460
2522
} else {
@@ -2493,7 +2555,7 @@ function DSLocalStorageAdapterProvider() {
2493
2555
var items = [ ] ;
2494
2556
var ids = DSUtils . keys ( _this . getIds ( resourceConfig . name , options ) ) ;
2495
2557
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 ) ) ;
2497
2559
if ( itemJson ) {
2498
2560
items . push ( DSUtils . fromJson ( itemJson ) ) ;
2499
2561
}
@@ -2536,15 +2598,19 @@ function DSLocalStorageAdapterProvider() {
2536
2598
*/
2537
2599
create : function ( resourceConfig , attrs , options ) {
2538
2600
var _this = this ;
2539
- attrs [ resourceConfig . idAttribute ] = attrs [ resourceConfig . idAttribute ] || DSUtils . guid ( ) ;
2601
+ var id = attrs [ resourceConfig . idAttribute ] ;
2540
2602
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
+ } ) ;
2548
2614
} ,
2549
2615
2550
2616
/**
@@ -2582,9 +2648,13 @@ function DSLocalStorageAdapterProvider() {
2582
2648
update : function ( resourceConfig , id , attrs , options ) {
2583
2649
options = options || { } ;
2584
2650
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 || { } ;
2586
2653
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 ;
2588
2658
} ) ;
2589
2659
} ,
2590
2660
@@ -2653,7 +2723,7 @@ function DSLocalStorageAdapterProvider() {
2653
2723
*/
2654
2724
destroy : function ( resourceConfig , id , options ) {
2655
2725
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 ) ) ;
2657
2727
} ,
2658
2728
2659
2729
/**
@@ -6814,7 +6884,6 @@ function _link(definition, injected, options) {
6814
6884
* the items that were injected into the data store.
6815
6885
*/
6816
6886
function inject ( resourceName , attrs , options ) {
6817
- console . log ( 'inject' , resourceName , attrs ) ;
6818
6887
var DS = this ;
6819
6888
var IA = DS . errors . IA ;
6820
6889
var definition = DS . definitions [ resourceName ] ;
@@ -6847,9 +6916,7 @@ function inject(resourceName, attrs, options) {
6847
6916
resource . collectionModified = DS . utils . updateTimestamp ( resource . collectionModified ) ;
6848
6917
}
6849
6918
6850
- console . log ( options ) ;
6851
6919
if ( options . linkInverse && typeof options . linkInverse === 'boolean' ) {
6852
- console . log ( 'linkInverse' , typeof options . linkInverse , options . linkInverse ) ;
6853
6920
if ( DS . utils . isArray ( injected ) ) {
6854
6921
if ( injected . length ) {
6855
6922
DS . linkInverse ( definition . name , injected [ 0 ] [ definition . idAttribute ] ) ;
@@ -6859,8 +6926,6 @@ function inject(resourceName, attrs, options) {
6859
6926
}
6860
6927
}
6861
6928
6862
- console . log ( injected ) ;
6863
-
6864
6929
if ( DS . utils . isArray ( injected ) ) {
6865
6930
DS . utils . forEach ( injected , function ( injectedI ) {
6866
6931
_link . call ( DS , definition , injectedI , options ) ;
@@ -7072,7 +7137,6 @@ function _link(definition, linked, relations) {
7072
7137
* @returns {object|array } A reference to the item with its linked relations.
7073
7138
*/
7074
7139
function link ( resourceName , id , relations ) {
7075
- console . log ( 'link' , resourceName , id ) ;
7076
7140
var DS = this ;
7077
7141
var IA = DS . errors . IA ;
7078
7142
var definition = DS . definitions [ resourceName ] ;
@@ -7099,8 +7163,6 @@ function link(resourceName, id, relations) {
7099
7163
}
7100
7164
}
7101
7165
7102
- console . log ( 'linked' , linked ) ;
7103
-
7104
7166
return linked ;
7105
7167
}
7106
7168
0 commit comments