11/**
22* @author Jason Dobry <jason.dobry@gmail.com>
33* @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/>
55* @copyright (c) 2014 Jason Dobry <https://github.com/jmdobry/>
66* @license MIT <https://github.com/jmdobry/angular-data/blob/master/LICENSE>
77*
@@ -4248,7 +4248,7 @@ function DSProvider() {
42484248
42494249module . exports = DSProvider ;
42504250
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 ) {
42524252function errorPrefix ( resourceName ) {
42534253 return 'DS.bindAll(scope, expr, ' + resourceName + ', params[, cb]): ' ;
42544254}
@@ -5032,6 +5032,7 @@ function _eject(definition, resource, id) {
50325032 }
50335033 }
50345034 if ( found ) {
5035+ this . unlinkInverse ( definition . name , id ) ;
50355036 resource . collection . splice ( i , 1 ) ;
50365037 resource . observers [ id ] . close ( ) ;
50375038 delete resource . observers [ id ] ;
@@ -5456,6 +5457,16 @@ module.exports = {
54565457 */
54575458 bindAll : require ( './bindAll' ) ,
54585459
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+
54595470 /**
54605471 * @doc method
54615472 * @id DS.sync methods:compute
@@ -5486,6 +5497,16 @@ module.exports = {
54865497 */
54875498 defineResource : require ( './defineResource' ) ,
54885499
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+
54895510 /**
54905511 * @doc method
54915512 * @id DS.sync methods:eject
@@ -5526,6 +5547,16 @@ module.exports = {
55265547 */
55275548 get : require ( './get' ) ,
55285549
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+
55295560 /**
55305561 * @doc method
55315562 * @id DS.sync methods:inject
@@ -5586,26 +5617,6 @@ module.exports = {
55865617 */
55875618 linkInverse : require ( './linkInverse' ) ,
55885619
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-
56095620 /**
56105621 * @doc method
56115622 * @id DS.sync methods:previous
@@ -5618,16 +5629,16 @@ module.exports = {
56185629
56195630 /**
56205631 * @doc method
5621- * @id DS.sync methods:hasChanges
5622- * @name hasChanges
5632+ * @id DS.sync methods:unlinkInverse
5633+ * @name unlinkInverse
56235634 * @methodOf DS
56245635 * @description
5625- * See [DS.hasChanges ](/documentation/api/api/DS.sync methods:hasChanges ).
5636+ * See [DS.unlinkInverse ](/documentation/api/api/DS.sync methods:unlinkInverse ).
56265637 */
5627- hasChanges : require ( './hasChanges ' )
5638+ unlinkInverse : require ( './unlinkInverse ' )
56285639} ;
56295640
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 ) {
56315642var observe = require ( '../../../lib/observe-js/observe-js' ) ;
56325643var _compute = require ( './compute' ) . _compute ;
56335644var stack = 0 ;
@@ -6404,6 +6415,104 @@ function previous(resourceName, id) {
64046415module . exports = previous ;
64056416
64066417} , { } ] , 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 ) {
64076516/**
64086517 * @doc function
64096518 * @id errors.types:IllegalArgumentError
@@ -6536,7 +6645,7 @@ module.exports = [function () {
65366645 } ;
65376646} ] ;
65386647
6539- } , { } ] , 71 :[ function ( require , module , exports ) {
6648+ } , { } ] , 72 :[ function ( require , module , exports ) {
65406649( function ( window , angular , undefined ) {
65416650 'use strict' ;
65426651
@@ -6545,7 +6654,7 @@ module.exports = [function () {
65456654 * @id angular-data
65466655 * @name angular-data
65476656 * @description
6548- * __Version:__ 1.0.0-beta.3
6657+ * __Version:__ 1.0.0-beta.4
65496658 *
65506659 * ## Install
65516660 *
@@ -6624,7 +6733,7 @@ module.exports = [function () {
66246733
66256734} ) ( window , window . angular ) ;
66266735
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 ) {
66286737module . exports = [ function ( ) {
66296738 return {
66306739 isBoolean : require ( 'mout/lang/isBoolean' ) ,
@@ -6709,4 +6818,4 @@ module.exports = [function () {
67096818 } ;
67106819} ] ;
67116820
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