11/**
22* @author Jason Dobry <jason.dobry@gmail.com>
33* @file angular-data.js
4- * @version 1.4.2 - Homepage <http://angular-data.pseudobry.com/>
4+ * @version 1.4.3 - 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*
@@ -6555,44 +6555,26 @@ function errorPrefix(resourceName) {
65556555 return 'DS.inject(' + resourceName + ', attrs[, options]): ' ;
65566556}
65576557
6558- function _injectRelations ( definition , injected , options ) {
6559- var DS = this ;
6560-
6561- DS . utils . forEach ( definition . relationList , function ( def ) {
6562- var relationName = def . relation ;
6563- var relationDef = DS . definitions [ relationName ] ;
6564- if ( injected [ def . localField ] ) {
6565- if ( ! relationDef ) {
6566- throw new DS . errors . R ( definition . name + 'relation is defined but the resource is not!' ) ;
6567- }
6568- try {
6569- injected [ def . localField ] = DS . inject ( relationName , injected [ def . localField ] , options ) ;
6570- } catch ( err ) {
6571- DS . $log . error ( errorPrefix ( definition . name ) + 'Failed to inject ' + def . type + ' relation: "' + relationName + '"!' , err ) ;
6572- }
6573- }
6574- } ) ;
6575- }
6576-
65776558function _inject ( definition , resource , attrs , options ) {
65786559 var DS = this ;
6560+ var DSUtils = DS . utils ;
65796561 var $log = DS . $log ;
65806562
65816563 function _react ( added , removed , changed , oldValueFn , firstTime ) {
65826564 var target = this ;
65836565 var item ;
65846566 var innerId = ( oldValueFn && oldValueFn ( definition . idAttribute ) ) ? oldValueFn ( definition . idAttribute ) : target [ definition . idAttribute ] ;
65856567
6586- DS . utils . forEach ( definition . relationFields , function ( field ) {
6568+ DSUtils . forEach ( definition . relationFields , function ( field ) {
65876569 delete added [ field ] ;
65886570 delete removed [ field ] ;
65896571 delete changed [ field ] ;
65906572 } ) ;
65916573
6592- if ( ! DS . utils . isEmpty ( added ) || ! DS . utils . isEmpty ( removed ) || ! DS . utils . isEmpty ( changed ) || firstTime ) {
6574+ if ( ! DSUtils . isEmpty ( added ) || ! DSUtils . isEmpty ( removed ) || ! DSUtils . isEmpty ( changed ) || firstTime ) {
65936575 item = DS . get ( definition . name , innerId ) ;
6594- resource . modified [ innerId ] = DS . utils . updateTimestamp ( resource . modified [ innerId ] ) ;
6595- resource . collectionModified = DS . utils . updateTimestamp ( resource . collectionModified ) ;
6576+ resource . modified [ innerId ] = DSUtils . updateTimestamp ( resource . modified [ innerId ] ) ;
6577+ resource . collectionModified = DSUtils . updateTimestamp ( resource . collectionModified ) ;
65966578 if ( definition . keepChangeHistory ) {
65976579 var changeRecord = {
65986580 resourceName : definition . name ,
@@ -6609,7 +6591,7 @@ function _inject(definition, resource, attrs, options) {
66096591
66106592 if ( definition . computed ) {
66116593 item = item || DS . get ( definition . name , innerId ) ;
6612- DS . utils . forEach ( definition . computed , function ( fn , field ) {
6594+ DSUtils . forEach ( definition . computed , function ( fn , field ) {
66136595 var compute = false ;
66146596 // check if required fields changed
66156597 angular . forEach ( fn . deps , function ( dep ) {
@@ -6626,7 +6608,7 @@ function _inject(definition, resource, attrs, options) {
66266608
66276609 if ( definition . relations ) {
66286610 item = item || DS . get ( definition . name , innerId ) ;
6629- DS . utils . forEach ( definition . relationList , function ( def ) {
6611+ DSUtils . forEach ( definition . relationList , function ( def ) {
66306612 if ( item [ def . localField ] && ( def . localKey in added || def . localKey in removed || def . localKey in changed ) ) {
66316613 DS . link ( definition . name , item [ definition . idAttribute ] , [ def . relation ] ) ;
66326614 }
@@ -6641,7 +6623,7 @@ function _inject(definition, resource, attrs, options) {
66416623 }
66426624
66436625 var injected ;
6644- if ( DS . utils . isArray ( attrs ) ) {
6626+ if ( DSUtils . isArray ( attrs ) ) {
66456627 injected = [ ] ;
66466628 for ( var i = 0 ; i < attrs . length ; i ++ ) {
66476629 injected . push ( _inject . call ( DS , definition , resource , attrs [ i ] , options ) ) ;
@@ -6663,6 +6645,45 @@ function _inject(definition, resource, attrs, options) {
66636645 throw error ;
66646646 } else {
66656647 try {
6648+ DSUtils . forEach ( definition . relationList , function ( def ) {
6649+ var relationName = def . relation ;
6650+ var relationDef = DS . definitions [ relationName ] ;
6651+ var toInject = attrs [ def . localField ] ;
6652+ if ( toInject ) {
6653+ if ( ! relationDef ) {
6654+ throw new DS . errors . R ( definition . name + 'relation is defined but the resource is not!' ) ;
6655+ }
6656+ if ( DSUtils . isArray ( toInject ) ) {
6657+ var items = [ ] ;
6658+ DSUtils . forEach ( toInject , function ( toInjectItem ) {
6659+ if ( toInjectItem !== DS . store [ relationName ] [ toInjectItem [ relationDef . idAttribute ] ] ) {
6660+ try {
6661+ var injectedItem = DS . inject ( relationName , toInjectItem , options ) ;
6662+ if ( def . foreignKey ) {
6663+ injectedItem [ def . foreignKey ] = attrs [ definition . idAttribute ] ;
6664+ }
6665+ items . push ( injectedItem ) ;
6666+ } catch ( err ) {
6667+ DS . $log . error ( errorPrefix ( definition . name ) + 'Failed to inject ' + def . type + ' relation: "' + relationName + '"!' , err ) ;
6668+ }
6669+ }
6670+ } ) ;
6671+ attrs [ def . localField ] = items ;
6672+ } else {
6673+ if ( toInject !== DS . store [ relationName ] [ toInject [ relationDef . idAttribute ] ] ) {
6674+ try {
6675+ attrs [ def . localField ] = DS . inject ( relationName , attrs [ def . localField ] , options ) ;
6676+ if ( def . foreignKey ) {
6677+ attrs [ def . localField ] [ def . foreignKey ] = attrs [ definition . idAttribute ] ;
6678+ }
6679+ } catch ( err ) {
6680+ DS . $log . error ( errorPrefix ( definition . name ) + 'Failed to inject ' + def . type + ' relation: "' + relationName + '"!' , err ) ;
6681+ }
6682+ }
6683+ }
6684+ }
6685+ } ) ;
6686+
66666687 definition . beforeInject ( definition . name , attrs ) ;
66676688 var id = attrs [ idA ] ;
66686689 var item = DS . get ( definition . name , id ) ;
@@ -6680,8 +6701,8 @@ function _inject(definition, resource, attrs, options) {
66806701 }
66816702 resource . previousAttributes [ id ] = { } ;
66826703
6683- DS . utils . deepMixIn ( item , attrs ) ;
6684- DS . utils . deepMixIn ( resource . previousAttributes [ id ] , attrs ) ;
6704+ DSUtils . deepMixIn ( item , attrs ) ;
6705+ DSUtils . deepMixIn ( resource . previousAttributes [ id ] , attrs ) ;
66856706
66866707 resource . collection . push ( item ) ;
66876708
@@ -6691,18 +6712,14 @@ function _inject(definition, resource, attrs, options) {
66916712 resource . index . put ( id , item ) ;
66926713
66936714 _react . call ( item , { } , { } , { } , null , true ) ;
6694-
6695- if ( definition . relations ) {
6696- _injectRelations . call ( DS , definition , item , options ) ;
6697- }
66986715 } else {
6699- DS . utils . deepMixIn ( item , attrs ) ;
6716+ DSUtils . deepMixIn ( item , attrs ) ;
67006717 if ( definition . resetHistoryOnInject ) {
67016718 resource . previousAttributes [ id ] = { } ;
6702- DS . utils . deepMixIn ( resource . previousAttributes [ id ] , attrs ) ;
6719+ DSUtils . deepMixIn ( resource . previousAttributes [ id ] , attrs ) ;
67036720 if ( resource . changeHistories [ id ] . length ) {
6704- DS . utils . forEach ( resource . changeHistories [ id ] , function ( changeRecord ) {
6705- DS . utils . remove ( resource . changeHistory , changeRecord ) ;
6721+ DSUtils . forEach ( resource . changeHistories [ id ] , function ( changeRecord ) {
6722+ DSUtils . remove ( resource . changeHistory , changeRecord ) ;
67066723 } ) ;
67076724 resource . changeHistories [ id ] . splice ( 0 , resource . changeHistories [ id ] . length ) ;
67086725 }
@@ -6714,8 +6731,9 @@ function _inject(definition, resource, attrs, options) {
67146731 }
67156732 resource . observers [ id ] . deliver ( ) ;
67166733 }
6717- resource . saved [ id ] = DS . utils . updateTimestamp ( resource . saved [ id ] ) ;
6718- resource . modified [ id ] = initialLastModified && resource . modified [ id ] === initialLastModified ? DS . utils . updateTimestamp ( resource . modified [ id ] ) : resource . modified [ id ] ;
6734+ resource . saved [ id ] = DSUtils . updateTimestamp ( resource . saved [ id ] ) ;
6735+ resource . modified [ id ] = initialLastModified && resource . modified [ id ] === initialLastModified ? DSUtils . updateTimestamp ( resource . modified [ id ] ) : resource . modified [ id ] ;
6736+
67196737 definition . afterInject ( definition . name , item ) ;
67206738 injected = item ;
67216739 } catch ( err ) {
@@ -6796,6 +6814,7 @@ function _link(definition, injected, options) {
67966814 * the items that were injected into the data store.
67976815 */
67986816function inject ( resourceName , attrs , options ) {
6817+ console . log ( 'inject' , resourceName , attrs ) ;
67996818 var DS = this ;
68006819 var IA = DS . errors . IA ;
68016820 var definition = DS . definitions [ resourceName ] ;
@@ -6828,7 +6847,9 @@ function inject(resourceName, attrs, options) {
68286847 resource . collectionModified = DS . utils . updateTimestamp ( resource . collectionModified ) ;
68296848 }
68306849
6831- if ( options . linkInverse ) {
6850+ console . log ( options ) ;
6851+ if ( options . linkInverse && typeof options . linkInverse === 'boolean' ) {
6852+ console . log ( 'linkInverse' , typeof options . linkInverse , options . linkInverse ) ;
68326853 if ( DS . utils . isArray ( injected ) ) {
68336854 if ( injected . length ) {
68346855 DS . linkInverse ( definition . name , injected [ 0 ] [ definition . idAttribute ] ) ;
@@ -6838,6 +6859,8 @@ function inject(resourceName, attrs, options) {
68386859 }
68396860 }
68406861
6862+ console . log ( injected ) ;
6863+
68416864 if ( DS . utils . isArray ( injected ) ) {
68426865 DS . utils . forEach ( injected , function ( injectedI ) {
68436866 _link . call ( DS , definition , injectedI , options ) ;
@@ -6850,7 +6873,6 @@ function inject(resourceName, attrs, options) {
68506873 DS . emit ( definition , 'inject' , injected ) ;
68516874 }
68526875
6853-
68546876 return injected ;
68556877}
68566878
@@ -7050,6 +7072,7 @@ function _link(definition, linked, relations) {
70507072 * @returns {object|array } A reference to the item with its linked relations.
70517073 */
70527074function link ( resourceName , id , relations ) {
7075+ console . log ( 'link' , resourceName , id ) ;
70537076 var DS = this ;
70547077 var IA = DS . errors . IA ;
70557078 var definition = DS . definitions [ resourceName ] ;
@@ -7076,6 +7099,8 @@ function link(resourceName, id, relations) {
70767099 }
70777100 }
70787101
7102+ console . log ( 'linked' , linked ) ;
7103+
70797104 return linked ;
70807105}
70817106
0 commit comments