1
1
/**
2
2
* @author Jason Dobry <[email protected] >
3
3
* @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/>
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
*
@@ -6555,44 +6555,26 @@ function errorPrefix(resourceName) {
6555
6555
return 'DS.inject(' + resourceName + ', attrs[, options]): ' ;
6556
6556
}
6557
6557
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
-
6577
6558
function _inject ( definition , resource , attrs , options ) {
6578
6559
var DS = this ;
6560
+ var DSUtils = DS . utils ;
6579
6561
var $log = DS . $log ;
6580
6562
6581
6563
function _react ( added , removed , changed , oldValueFn , firstTime ) {
6582
6564
var target = this ;
6583
6565
var item ;
6584
6566
var innerId = ( oldValueFn && oldValueFn ( definition . idAttribute ) ) ? oldValueFn ( definition . idAttribute ) : target [ definition . idAttribute ] ;
6585
6567
6586
- DS . utils . forEach ( definition . relationFields , function ( field ) {
6568
+ DSUtils . forEach ( definition . relationFields , function ( field ) {
6587
6569
delete added [ field ] ;
6588
6570
delete removed [ field ] ;
6589
6571
delete changed [ field ] ;
6590
6572
} ) ;
6591
6573
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 ) {
6593
6575
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 ) ;
6596
6578
if ( definition . keepChangeHistory ) {
6597
6579
var changeRecord = {
6598
6580
resourceName : definition . name ,
@@ -6609,7 +6591,7 @@ function _inject(definition, resource, attrs, options) {
6609
6591
6610
6592
if ( definition . computed ) {
6611
6593
item = item || DS . get ( definition . name , innerId ) ;
6612
- DS . utils . forEach ( definition . computed , function ( fn , field ) {
6594
+ DSUtils . forEach ( definition . computed , function ( fn , field ) {
6613
6595
var compute = false ;
6614
6596
// check if required fields changed
6615
6597
angular . forEach ( fn . deps , function ( dep ) {
@@ -6626,7 +6608,7 @@ function _inject(definition, resource, attrs, options) {
6626
6608
6627
6609
if ( definition . relations ) {
6628
6610
item = item || DS . get ( definition . name , innerId ) ;
6629
- DS . utils . forEach ( definition . relationList , function ( def ) {
6611
+ DSUtils . forEach ( definition . relationList , function ( def ) {
6630
6612
if ( item [ def . localField ] && ( def . localKey in added || def . localKey in removed || def . localKey in changed ) ) {
6631
6613
DS . link ( definition . name , item [ definition . idAttribute ] , [ def . relation ] ) ;
6632
6614
}
@@ -6641,7 +6623,7 @@ function _inject(definition, resource, attrs, options) {
6641
6623
}
6642
6624
6643
6625
var injected ;
6644
- if ( DS . utils . isArray ( attrs ) ) {
6626
+ if ( DSUtils . isArray ( attrs ) ) {
6645
6627
injected = [ ] ;
6646
6628
for ( var i = 0 ; i < attrs . length ; i ++ ) {
6647
6629
injected . push ( _inject . call ( DS , definition , resource , attrs [ i ] , options ) ) ;
@@ -6663,6 +6645,45 @@ function _inject(definition, resource, attrs, options) {
6663
6645
throw error ;
6664
6646
} else {
6665
6647
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
+
6666
6687
definition . beforeInject ( definition . name , attrs ) ;
6667
6688
var id = attrs [ idA ] ;
6668
6689
var item = DS . get ( definition . name , id ) ;
@@ -6680,8 +6701,8 @@ function _inject(definition, resource, attrs, options) {
6680
6701
}
6681
6702
resource . previousAttributes [ id ] = { } ;
6682
6703
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 ) ;
6685
6706
6686
6707
resource . collection . push ( item ) ;
6687
6708
@@ -6691,18 +6712,14 @@ function _inject(definition, resource, attrs, options) {
6691
6712
resource . index . put ( id , item ) ;
6692
6713
6693
6714
_react . call ( item , { } , { } , { } , null , true ) ;
6694
-
6695
- if ( definition . relations ) {
6696
- _injectRelations . call ( DS , definition , item , options ) ;
6697
- }
6698
6715
} else {
6699
- DS . utils . deepMixIn ( item , attrs ) ;
6716
+ DSUtils . deepMixIn ( item , attrs ) ;
6700
6717
if ( definition . resetHistoryOnInject ) {
6701
6718
resource . previousAttributes [ id ] = { } ;
6702
- DS . utils . deepMixIn ( resource . previousAttributes [ id ] , attrs ) ;
6719
+ DSUtils . deepMixIn ( resource . previousAttributes [ id ] , attrs ) ;
6703
6720
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 ) ;
6706
6723
} ) ;
6707
6724
resource . changeHistories [ id ] . splice ( 0 , resource . changeHistories [ id ] . length ) ;
6708
6725
}
@@ -6714,8 +6731,9 @@ function _inject(definition, resource, attrs, options) {
6714
6731
}
6715
6732
resource . observers [ id ] . deliver ( ) ;
6716
6733
}
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
+
6719
6737
definition . afterInject ( definition . name , item ) ;
6720
6738
injected = item ;
6721
6739
} catch ( err ) {
@@ -6796,6 +6814,7 @@ function _link(definition, injected, options) {
6796
6814
* the items that were injected into the data store.
6797
6815
*/
6798
6816
function inject ( resourceName , attrs , options ) {
6817
+ console . log ( 'inject' , resourceName , attrs ) ;
6799
6818
var DS = this ;
6800
6819
var IA = DS . errors . IA ;
6801
6820
var definition = DS . definitions [ resourceName ] ;
@@ -6828,7 +6847,9 @@ function inject(resourceName, attrs, options) {
6828
6847
resource . collectionModified = DS . utils . updateTimestamp ( resource . collectionModified ) ;
6829
6848
}
6830
6849
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 ) ;
6832
6853
if ( DS . utils . isArray ( injected ) ) {
6833
6854
if ( injected . length ) {
6834
6855
DS . linkInverse ( definition . name , injected [ 0 ] [ definition . idAttribute ] ) ;
@@ -6838,6 +6859,8 @@ function inject(resourceName, attrs, options) {
6838
6859
}
6839
6860
}
6840
6861
6862
+ console . log ( injected ) ;
6863
+
6841
6864
if ( DS . utils . isArray ( injected ) ) {
6842
6865
DS . utils . forEach ( injected , function ( injectedI ) {
6843
6866
_link . call ( DS , definition , injectedI , options ) ;
@@ -6850,7 +6873,6 @@ function inject(resourceName, attrs, options) {
6850
6873
DS . emit ( definition , 'inject' , injected ) ;
6851
6874
}
6852
6875
6853
-
6854
6876
return injected ;
6855
6877
}
6856
6878
@@ -7050,6 +7072,7 @@ function _link(definition, linked, relations) {
7050
7072
* @returns {object|array } A reference to the item with its linked relations.
7051
7073
*/
7052
7074
function link ( resourceName , id , relations ) {
7075
+ console . log ( 'link' , resourceName , id ) ;
7053
7076
var DS = this ;
7054
7077
var IA = DS . errors . IA ;
7055
7078
var definition = DS . definitions [ resourceName ] ;
@@ -7076,6 +7099,8 @@ function link(resourceName, id, relations) {
7076
7099
}
7077
7100
}
7078
7101
7102
+ console . log ( 'linked' , linked ) ;
7103
+
7079
7104
return linked ;
7080
7105
}
7081
7106
0 commit comments