1
1
/**
2
2
* @author Jason Dobry <[email protected] >
3
3
* @file angular-data.js
4
- * @version 1.0.0-beta.3 - Homepage <http://angular-data.pseudobry.com/>
4
+ * @version 1.0.0-beta.4 - 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
*
@@ -4248,7 +4248,7 @@ function DSProvider() {
4248
4248
4249
4249
module . exports = DSProvider ;
4250
4250
4251
- } , { "../utils" :72 , "./async_methods" :43 , "./sync_methods" :62 } ] , 50 :[ function ( require , module , exports ) {
4251
+ } , { "../utils" :73 , "./async_methods" :43 , "./sync_methods" :62 } ] , 50 :[ function ( require , module , exports ) {
4252
4252
function errorPrefix ( resourceName ) {
4253
4253
return 'DS.bindAll(scope, expr, ' + resourceName + ', params[, cb]): ' ;
4254
4254
}
@@ -5032,6 +5032,7 @@ function _eject(definition, resource, id) {
5032
5032
}
5033
5033
}
5034
5034
if ( found ) {
5035
+ this . unlinkInverse ( definition . name , id ) ;
5035
5036
resource . collection . splice ( i , 1 ) ;
5036
5037
resource . observers [ id ] . close ( ) ;
5037
5038
delete resource . observers [ id ] ;
@@ -5456,6 +5457,16 @@ module.exports = {
5456
5457
*/
5457
5458
bindAll : require ( './bindAll' ) ,
5458
5459
5460
+ /**
5461
+ * @doc method
5462
+ * @id DS.sync methods:changes
5463
+ * @name changes
5464
+ * @methodOf DS
5465
+ * @description
5466
+ * See [DS.changes](/documentation/api/api/DS.sync methods:changes).
5467
+ */
5468
+ changes : require ( './changes' ) ,
5469
+
5459
5470
/**
5460
5471
* @doc method
5461
5472
* @id DS.sync methods:compute
@@ -5486,6 +5497,16 @@ module.exports = {
5486
5497
*/
5487
5498
defineResource : require ( './defineResource' ) ,
5488
5499
5500
+ /**
5501
+ * @doc method
5502
+ * @id DS.sync methods:digest
5503
+ * @name digest
5504
+ * @methodOf DS
5505
+ * @description
5506
+ * See [DS.digest](/documentation/api/api/DS.sync methods:digest).
5507
+ */
5508
+ digest : require ( './digest' ) ,
5509
+
5489
5510
/**
5490
5511
* @doc method
5491
5512
* @id DS.sync methods:eject
@@ -5526,6 +5547,16 @@ module.exports = {
5526
5547
*/
5527
5548
get : require ( './get' ) ,
5528
5549
5550
+ /**
5551
+ * @doc method
5552
+ * @id DS.sync methods:hasChanges
5553
+ * @name hasChanges
5554
+ * @methodOf DS
5555
+ * @description
5556
+ * See [DS.hasChanges](/documentation/api/api/DS.sync methods:hasChanges).
5557
+ */
5558
+ hasChanges : require ( './hasChanges' ) ,
5559
+
5529
5560
/**
5530
5561
* @doc method
5531
5562
* @id DS.sync methods:inject
@@ -5586,26 +5617,6 @@ module.exports = {
5586
5617
*/
5587
5618
linkInverse : require ( './linkInverse' ) ,
5588
5619
5589
- /**
5590
- * @doc method
5591
- * @id DS.sync methods:digest
5592
- * @name digest
5593
- * @methodOf DS
5594
- * @description
5595
- * See [DS.digest](/documentation/api/api/DS.sync methods:digest).
5596
- */
5597
- digest : require ( './digest' ) ,
5598
-
5599
- /**
5600
- * @doc method
5601
- * @id DS.sync methods:changes
5602
- * @name changes
5603
- * @methodOf DS
5604
- * @description
5605
- * See [DS.changes](/documentation/api/api/DS.sync methods:changes).
5606
- */
5607
- changes : require ( './changes' ) ,
5608
-
5609
5620
/**
5610
5621
* @doc method
5611
5622
* @id DS.sync methods:previous
@@ -5618,16 +5629,16 @@ module.exports = {
5618
5629
5619
5630
/**
5620
5631
* @doc method
5621
- * @id DS.sync methods:hasChanges
5622
- * @name hasChanges
5632
+ * @id DS.sync methods:unlinkInverse
5633
+ * @name unlinkInverse
5623
5634
* @methodOf DS
5624
5635
* @description
5625
- * See [DS.hasChanges ](/documentation/api/api/DS.sync methods:hasChanges ).
5636
+ * See [DS.unlinkInverse ](/documentation/api/api/DS.sync methods:unlinkInverse ).
5626
5637
*/
5627
- hasChanges : require ( './hasChanges ' )
5638
+ unlinkInverse : require ( './unlinkInverse ' )
5628
5639
} ;
5629
5640
5630
- } , { "./bindAll" :50 , "./bindOne" :51 , "./changes" :52 , "./compute" :53 , "./createInstance" :54 , "./defineResource" :55 , "./digest" :56 , "./eject" :57 , "./ejectAll" :58 , "./filter" :59 , "./get" :60 , "./hasChanges" :61 , "./inject" :63 , "./lastModified" :64 , "./lastSaved" :65 , "./link" :66 , "./linkAll" :67 , "./linkInverse" :68 , "./previous" :69 } ] , 63 :[ function ( require , module , exports ) {
5641
+ } , { "./bindAll" :50 , "./bindOne" :51 , "./changes" :52 , "./compute" :53 , "./createInstance" :54 , "./defineResource" :55 , "./digest" :56 , "./eject" :57 , "./ejectAll" :58 , "./filter" :59 , "./get" :60 , "./hasChanges" :61 , "./inject" :63 , "./lastModified" :64 , "./lastSaved" :65 , "./link" :66 , "./linkAll" :67 , "./linkInverse" :68 , "./previous" :69 , "./unlinkInverse" : 70 } ] , 63 :[ function ( require , module , exports ) {
5631
5642
var observe = require ( '../../../lib/observe-js/observe-js' ) ;
5632
5643
var _compute = require ( './compute' ) . _compute ;
5633
5644
var stack = 0 ;
@@ -6404,6 +6415,104 @@ function previous(resourceName, id) {
6404
6415
module . exports = previous ;
6405
6416
6406
6417
} , { } ] , 70 :[ function ( require , module , exports ) {
6418
+ function errorPrefix ( resourceName ) {
6419
+ return 'DS.unlinkInverse(' + resourceName + ', id[, relations]): ' ;
6420
+ }
6421
+
6422
+ function _unlinkInverse ( definition , linked ) {
6423
+ var DS = this ;
6424
+ DS . utils . forOwn ( DS . definitions , function ( d ) {
6425
+ DS . utils . forOwn ( d . relations , function ( relatedModels ) {
6426
+ DS . utils . forOwn ( relatedModels , function ( defs , relationName ) {
6427
+ if ( definition . name === relationName ) {
6428
+ DS . utils . forEach ( defs , function ( def ) {
6429
+ DS . utils . forEach ( DS . store [ def . name ] . collection , function ( item ) {
6430
+ if ( def . type === 'hasMany' && item [ def . localField ] ) {
6431
+ var index ;
6432
+ DS . utils . forEach ( item [ def . localField ] , function ( subItem , i ) {
6433
+ if ( subItem === linked ) {
6434
+ index = i ;
6435
+ }
6436
+ } ) ;
6437
+ item [ def . localField ] . splice ( index , 1 ) ;
6438
+ } else if ( item [ def . localField ] === linked ) {
6439
+ delete item [ def . localField ] ;
6440
+ }
6441
+ } ) ;
6442
+ } ) ;
6443
+ }
6444
+ } ) ;
6445
+ } ) ;
6446
+ } ) ;
6447
+ }
6448
+
6449
+ /**
6450
+ * @doc method
6451
+ * @id DS.sync methods:unlinkInverse
6452
+ * @name unlinkInverse
6453
+ * @description
6454
+ * Find relations of the item with the given primary key that are already in the data store and _unlink_ this item from those
6455
+ * relations. This unlinks links that would be created by `DS.linkInverse`.
6456
+ *
6457
+ * ## Signature:
6458
+ * ```js
6459
+ * DS.unlinkInverse(resourceName, id[, relations])
6460
+ * ```
6461
+ *
6462
+ * ## Examples:
6463
+ *
6464
+ * Assume `organization` has `hasMany` relationship to `user` and `post` has a `belongsTo` relationship to `user`.
6465
+ * ```js
6466
+ * DS.get('organization', 5); // { id: 5, users: [{ organizationId: 5, id: 1 }] }
6467
+ *
6468
+ * // unlink user 1 from its relations
6469
+ * DS.unlinkInverse('user', 1, ['organization']);
6470
+ *
6471
+ * DS.get('organization', 5); // { id: 5, users: [] }
6472
+ * ```
6473
+ *
6474
+ * ## Throws
6475
+ *
6476
+ * - `{IllegalArgumentError}`
6477
+ * - `{NonexistentResourceError}`
6478
+ *
6479
+ * @param {string } resourceName The resource type, e.g. 'user', 'comment', etc.
6480
+ * @param {string|number } id The primary key of the item for which to unlink relations.
6481
+ * @param {array= } relations The relations to be unlinked. If not provided then all relations will be unlinked. Default: `[]`.
6482
+ * @returns {object|array } A reference to the item that has been unlinked.
6483
+ */
6484
+ function unlinkInverse ( resourceName , id , relations ) {
6485
+ var DS = this ;
6486
+ var IA = DS . errors . IA ;
6487
+ var definition = DS . definitions [ resourceName ] ;
6488
+
6489
+ relations = relations || [ ] ;
6490
+
6491
+ if ( ! definition ) {
6492
+ throw new DS . errors . NER ( errorPrefix ( resourceName ) + resourceName ) ;
6493
+ } else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
6494
+ throw new IA ( errorPrefix ( resourceName ) + 'id: Must be a string or a number!' ) ;
6495
+ } else if ( ! DS . utils . isArray ( relations ) ) {
6496
+ throw new IA ( errorPrefix ( resourceName ) + 'relations: Must be an array!' ) ;
6497
+ }
6498
+ var linked = DS . get ( resourceName , id ) ;
6499
+
6500
+ if ( linked ) {
6501
+ if ( ! DS . $rootScope . $$phase ) {
6502
+ DS . $rootScope . $apply ( function ( ) {
6503
+ _unlinkInverse . call ( DS , definition , linked , relations ) ;
6504
+ } ) ;
6505
+ } else {
6506
+ _unlinkInverse . call ( DS , definition , linked , relations ) ;
6507
+ }
6508
+ }
6509
+
6510
+ return linked ;
6511
+ }
6512
+
6513
+ module . exports = unlinkInverse ;
6514
+
6515
+ } , { } ] , 71 :[ function ( require , module , exports ) {
6407
6516
/**
6408
6517
* @doc function
6409
6518
* @id errors.types:IllegalArgumentError
@@ -6536,7 +6645,7 @@ module.exports = [function () {
6536
6645
} ;
6537
6646
} ] ;
6538
6647
6539
- } , { } ] , 71 :[ function ( require , module , exports ) {
6648
+ } , { } ] , 72 :[ function ( require , module , exports ) {
6540
6649
( function ( window , angular , undefined ) {
6541
6650
'use strict' ;
6542
6651
@@ -6545,7 +6654,7 @@ module.exports = [function () {
6545
6654
* @id angular-data
6546
6655
* @name angular-data
6547
6656
* @description
6548
- * __Version:__ 1.0.0-beta.3
6657
+ * __Version:__ 1.0.0-beta.4
6549
6658
*
6550
6659
* ## Install
6551
6660
*
@@ -6624,7 +6733,7 @@ module.exports = [function () {
6624
6733
6625
6734
} ) ( window , window . angular ) ;
6626
6735
6627
- } , { "./adapters/http" :36 , "./adapters/localStorage" :37 , "./datastore" :49 , "./errors" :70 , "./utils" :72 } ] , 72 :[ function ( require , module , exports ) {
6736
+ } , { "./adapters/http" :36 , "./adapters/localStorage" :37 , "./datastore" :49 , "./errors" :71 , "./utils" :73 } ] , 73 :[ function ( require , module , exports ) {
6628
6737
module . exports = [ function ( ) {
6629
6738
return {
6630
6739
isBoolean : require ( 'mout/lang/isBoolean' ) ,
@@ -6709,4 +6818,4 @@ module.exports = [function () {
6709
6818
} ;
6710
6819
} ] ;
6711
6820
6712
- } , { "mout/array/contains" :2 , "mout/array/filter" :3 , "mout/array/slice" :7 , "mout/array/sort" :8 , "mout/array/toLookup" :9 , "mout/lang/isBoolean" :14 , "mout/lang/isEmpty" :15 , "mout/object/deepMixIn" :22 , "mout/object/forOwn" :24 , "mout/object/pick" :27 , "mout/object/set" :28 , "mout/string/makePath" :31 , "mout/string/pascalCase" :32 , "mout/string/upperCase" :35 } ] } , { } , [ 71 ] ) ;
6821
+ } , { "mout/array/contains" :2 , "mout/array/filter" :3 , "mout/array/slice" :7 , "mout/array/sort" :8 , "mout/array/toLookup" :9 , "mout/lang/isBoolean" :14 , "mout/lang/isEmpty" :15 , "mout/object/deepMixIn" :22 , "mout/object/forOwn" :24 , "mout/object/pick" :27 , "mout/object/set" :28 , "mout/string/makePath" :31 , "mout/string/pascalCase" :32 , "mout/string/upperCase" :35 } ] } , { } , [ 72 ] ) ;
0 commit comments