1
- /*!
2
- * js-data-localstorage
3
- * @version 2.2.0 - Homepage <http://www.js-data.io/docs/dslocalstorageadapter>
4
- * @author Jason Dobry <[email protected] >
5
- * @copyright (c) 2014-2015 Jason Dobry
6
- * @license MIT <https://github.com/js-data/js-data-localstorage/blob/master/LICENSE>
7
- *
8
- * @overview localStorage adapter for js-data.
9
- */
10
1
( function webpackUniversalModuleDefinition ( root , factory ) {
11
2
if ( typeof exports === 'object' && typeof module === 'object' )
12
3
module . exports = factory ( require ( "js-data" ) ) ;
@@ -138,11 +129,13 @@ return /******/ (function(modules) { // webpackBootstrap
138
129
_createClass ( DSLocalStorageAdapter , [ {
139
130
key : 'getPath' ,
140
131
value : function getPath ( resourceConfig , options ) {
132
+ options = options || { } ;
141
133
return DSUtils . makePath ( options . basePath || this . defaults . basePath || resourceConfig . basePath , resourceConfig . name ) ;
142
134
}
143
135
} , {
144
136
key : 'getIdPath' ,
145
137
value : function getIdPath ( resourceConfig , options , id ) {
138
+ options = options || { } ;
146
139
return DSUtils . makePath ( options . basePath || this . defaults . basePath || resourceConfig . basePath , resourceConfig . endpoint , id ) ;
147
140
}
148
141
} , {
@@ -168,14 +161,32 @@ return /******/ (function(modules) { // webpackBootstrap
168
161
key : 'ensureId' ,
169
162
value : function ensureId ( id , resourceConfig , options ) {
170
163
var ids = this . getIds ( resourceConfig , options ) ;
171
- ids [ id ] = 1 ;
164
+ if ( DSUtils . isArray ( id ) ) {
165
+ if ( ! id . length ) {
166
+ return ;
167
+ }
168
+ DSUtils . forEach ( id , function ( _id ) {
169
+ ids [ _id ] = 1 ;
170
+ } ) ;
171
+ } else {
172
+ ids [ id ] = 1 ;
173
+ }
172
174
this . saveKeys ( ids , resourceConfig , options ) ;
173
175
}
174
176
} , {
175
177
key : 'removeId' ,
176
178
value : function removeId ( id , resourceConfig , options ) {
177
179
var ids = this . getIds ( resourceConfig , options ) ;
178
- delete ids [ id ] ;
180
+ if ( DSUtils . isArray ( id ) ) {
181
+ if ( ! id . length ) {
182
+ return ;
183
+ }
184
+ DSUtils . forEach ( id , function ( _id ) {
185
+ delete ids [ _id ] ;
186
+ } ) ;
187
+ } else {
188
+ delete ids [ id ] ;
189
+ }
179
190
this . saveKeys ( ids , resourceConfig , options ) ;
180
191
}
181
192
} , {
@@ -458,16 +469,36 @@ return /******/ (function(modules) { // webpackBootstrap
458
469
} ) ;
459
470
} ) ;
460
471
}
472
+ } , {
473
+ key : 'createMany' ,
474
+ value : function createMany ( resourceConfig , items , options ) {
475
+ var _this7 = this ;
476
+
477
+ return createTask ( function ( resolve , reject ) {
478
+ queueTask ( function ( ) {
479
+ var tasks = [ ] ;
480
+ var ids = [ ] ;
481
+ DSUtils . forEach ( items , function ( attrs ) {
482
+ var id = attrs [ resourceConfig . idAttribute ] = attrs [ resourceConfig . idAttribute ] || guid ( ) ;
483
+ ids . push ( id ) ;
484
+ options = options || { } ;
485
+ tasks . push ( _this7 . PUT ( DSUtils . makePath ( _this7 . getIdPath ( resourceConfig , options , id ) ) , DSUtils . omit ( attrs , resourceConfig . relationFields || [ ] ) ) ) ;
486
+ } ) ;
487
+ _this7 . ensureId ( ids , resourceConfig , options ) ;
488
+ return DSUtils . Promise . all ( tasks ) . then ( resolve ) . catch ( reject ) ;
489
+ } ) ;
490
+ } ) ;
491
+ }
461
492
} , {
462
493
key : 'update' ,
463
494
value : function update ( resourceConfig , id , attrs , options ) {
464
- var _this7 = this ;
495
+ var _this8 = this ;
465
496
466
497
return createTask ( function ( resolve , reject ) {
467
498
queueTask ( function ( ) {
468
499
options = options || { } ;
469
- _this7 . PUT ( _this7 . getIdPath ( resourceConfig , options , id ) , DSUtils . omit ( attrs , resourceConfig . relationFields || [ ] ) ) . then ( function ( item ) {
470
- _this7 . ensureId ( item [ resourceConfig . idAttribute ] , resourceConfig , options ) ;
500
+ _this8 . PUT ( _this8 . getIdPath ( resourceConfig , options , id ) , DSUtils . omit ( attrs , resourceConfig . relationFields || [ ] ) ) . then ( function ( item ) {
501
+ _this8 . ensureId ( item [ resourceConfig . idAttribute ] , resourceConfig , options ) ;
471
502
resolve ( item ) ;
472
503
} ) . catch ( reject ) ;
473
504
} ) ;
@@ -476,26 +507,26 @@ return /******/ (function(modules) { // webpackBootstrap
476
507
} , {
477
508
key : 'updateAll' ,
478
509
value : function updateAll ( resourceConfig , attrs , params , options ) {
479
- var _this8 = this ;
510
+ var _this9 = this ;
480
511
481
512
return this . findAll ( resourceConfig , params , options ) . then ( function ( items ) {
482
513
var tasks = [ ] ;
483
514
DSUtils . forEach ( items , function ( item ) {
484
- return tasks . push ( _this8 . update ( resourceConfig , item [ resourceConfig . idAttribute ] , DSUtils . omit ( attrs , resourceConfig . relationFields || [ ] ) , options ) ) ;
515
+ return tasks . push ( _this9 . update ( resourceConfig , item [ resourceConfig . idAttribute ] , DSUtils . omit ( attrs , resourceConfig . relationFields || [ ] ) , options ) ) ;
485
516
} ) ;
486
517
return DSUtils . Promise . all ( tasks ) ;
487
518
} ) ;
488
519
}
489
520
} , {
490
521
key : 'destroy' ,
491
522
value : function destroy ( resourceConfig , id , options ) {
492
- var _this9 = this ;
523
+ var _this10 = this ;
493
524
494
525
return createTask ( function ( resolve , reject ) {
495
526
queueTask ( function ( ) {
496
527
options = options || { } ;
497
- _this9 . DEL ( _this9 . getIdPath ( resourceConfig , options , id ) ) . then ( function ( ) {
498
- return _this9 . removeId ( id , resourceConfig . name , options ) ;
528
+ _this10 . DEL ( _this10 . getIdPath ( resourceConfig , options , id ) ) . then ( function ( ) {
529
+ return _this10 . removeId ( id , resourceConfig , options ) ;
499
530
} ) . then ( function ( ) {
500
531
return resolve ( null ) ;
501
532
} , reject ) ;
@@ -505,14 +536,17 @@ return /******/ (function(modules) { // webpackBootstrap
505
536
} , {
506
537
key : 'destroyAll' ,
507
538
value : function destroyAll ( resourceConfig , params , options ) {
508
- var _this10 = this ;
539
+ var _this11 = this ;
509
540
510
541
return this . findAll ( resourceConfig , params , options ) . then ( function ( items ) {
511
- var tasks = [ ] ;
542
+ var ids = [ ] ;
512
543
DSUtils . forEach ( items , function ( item ) {
513
- return tasks . push ( _this10 . destroy ( resourceConfig , item [ resourceConfig . idAttribute ] , options ) ) ;
544
+ var id = item [ resourceConfig . idAttribute ] ;
545
+ ids . push ( id ) ;
546
+ _this11 . storage . removeItem ( _this11 . getIdPath ( resourceConfig , options , id ) ) ;
514
547
} ) ;
515
- return DSUtils . Promise . all ( tasks ) ;
548
+ _this11 . removeId ( ids , resourceConfig , options ) ;
549
+ return ids ;
516
550
} ) ;
517
551
}
518
552
} ] ) ;
@@ -521,12 +555,12 @@ return /******/ (function(modules) { // webpackBootstrap
521
555
} ) ( ) ;
522
556
523
557
DSLocalStorageAdapter . version = {
524
- full : '2.2.0 ' ,
525
- major : parseInt ( '2 ' , 10 ) ,
526
- minor : parseInt ( '2 ' , 10 ) ,
527
- patch : parseInt ( '0 ' , 10 ) ,
528
- alpha : true ? 'false ' : false ,
529
- beta : true ? 'false ' : false
558
+ full : '<%= pkg.version %> ' ,
559
+ major : parseInt ( '<%= major %> ' , 10 ) ,
560
+ minor : parseInt ( '<%= minor %> ' , 10 ) ,
561
+ patch : parseInt ( '<%= patch %> ' , 10 ) ,
562
+ alpha : true ? '<%= alpha %> ' : false ,
563
+ beta : true ? '<%= beta %> ' : false
530
564
} ;
531
565
532
566
module . exports = DSLocalStorageAdapter ;
0 commit comments