@@ -4,7 +4,7 @@ IonicModule
4
4
'$parse' ,
5
5
'$rootScope' ,
6
6
function ( $cacheFactory , $parse , $rootScope ) {
7
- var nextCacheId = 0 ;
7
+
8
8
function CollectionRepeatDataSource ( options ) {
9
9
var self = this ;
10
10
this . scope = options . scope ;
@@ -35,33 +35,28 @@ function($cacheFactory, $parse, $rootScope) {
35
35
} ;
36
36
}
37
37
38
- var cacheKeys = { } ;
39
- this . itemCache = $cacheFactory ( nextCacheId ++ , { size : 500 } ) ;
40
-
41
- var _put = this . itemCache . put ;
42
- this . itemCache . put = function ( key , value ) {
43
- cacheKeys [ key ] = true ;
44
- return _put ( key , value ) ;
45
- } ;
46
-
47
- var _remove = this . itemCache . remove ;
48
- this . itemCache . remove = function ( key ) {
49
- delete cacheKeys [ key ] ;
50
- return _remove ( key ) ;
51
- } ;
52
- this . itemCache . keys = function ( ) {
53
- return Object . keys ( cacheKeys ) ;
54
- } ;
38
+ this . attachedItems = { } ;
39
+ this . BACKUP_ITEMS_LENGTH = 10 ;
40
+ this . backupItemsArray = [ ] ;
55
41
}
56
42
CollectionRepeatDataSource . prototype = {
43
+ setup : function ( ) {
44
+ for ( var i = 0 ; i < this . BACKUP_ITEMS_LENGTH ; i ++ ) {
45
+ this . detachItem ( this . createItem ( ) ) ;
46
+ }
47
+ } ,
57
48
destroy : function ( ) {
58
49
this . dimensions . length = 0 ;
59
- this . itemCache . keys ( ) . forEach ( function ( key ) {
60
- var item = this . itemCache . get ( key ) ;
61
- item . element . remove ( ) ;
62
- item . scope . $destroy ( ) ;
50
+ this . data = null ;
51
+ forEach ( this . backupItemsArray , function ( item ) {
52
+ this . destroyItem ( item ) ;
53
+ } , this ) ;
54
+ this . backupItemsArray . length = 0 ;
55
+
56
+ forEach ( this . attachedItems , function ( item , key ) {
57
+ this . destroyItem ( item ) ;
63
58
} , this ) ;
64
- this . itemCache . removeAll ( ) ;
59
+ this . attachedItems = { } ;
65
60
} ,
66
61
calculateDataDimensions : function ( ) {
67
62
var locals = { } ;
@@ -74,53 +69,74 @@ function($cacheFactory, $parse, $rootScope) {
74
69
} ;
75
70
} , this ) ;
76
71
} ,
77
- compileItem : function ( index , value ) {
78
- var key = this . itemHashGetter ( index , value ) ;
79
- var cachedItem = this . itemCache . get ( key ) ;
80
- if ( cachedItem ) return cachedItem ;
81
-
72
+ createItem : function ( ) {
82
73
var item = { } ;
83
74
item . scope = this . scope . $new ( ) ;
84
- item . scope [ this . keyExpr ] = value ;
85
75
86
76
this . transcludeFn ( item . scope , function ( clone ) {
87
77
clone . css ( 'position' , 'absolute' ) ;
88
78
item . element = clone ;
89
79
} ) ;
90
80
91
- return this . itemCache . put ( key , item ) ;
81
+ this . transcludeParent . append ( item . element ) ;
82
+
83
+ return item ;
92
84
} ,
93
- getItem : function ( index ) {
85
+ getItem : function ( hash ) {
86
+ window . AMOUNT = window . AMOUNT || 0 ;
87
+ if ( ( item = this . attachedItems [ hash ] ) ) {
88
+ //do nothing, the item is good
89
+ } else if ( ( item = this . backupItemsArray . pop ( ) ) ) {
90
+ reconnectScope ( item . scope ) ;
91
+ } else {
92
+ AMOUNT ++ ;
93
+ item = this . createItem ( ) ;
94
+ }
95
+ return item ;
96
+ } ,
97
+ attachItemAtIndex : function ( index ) {
94
98
var value = this . data [ index ] ;
95
- var item = this . compileItem ( index , value ) ;
99
+ var hash = this . itemHashGetter ( index , value ) ;
100
+ var item = this . getItem ( hash ) ;
96
101
97
- if ( item . scope . $index !== index ) {
102
+ if ( item . scope . $index !== index || item . scope [ this . keyExpr ] !== value ) {
103
+ item . scope [ this . keyExpr ] = value ;
98
104
item . scope . $index = index ;
99
105
item . scope . $first = ( index === 0 ) ;
100
106
item . scope . $last = ( index === ( this . getLength ( ) - 1 ) ) ;
101
107
item . scope . $middle = ! ( item . scope . $first || item . scope . $last ) ;
102
108
item . scope . $odd = ! ( item . scope . $even = ( index & 1 ) === 0 ) ;
109
+
110
+ //We changed the scope, so digest if needed
111
+ if ( ! $rootScope . $$phase ) {
112
+ item . scope . $digest ( ) ;
113
+ }
103
114
}
104
115
116
+ item . hash = hash ;
117
+ this . attachedItems [ hash ] = item ;
118
+
105
119
return item ;
106
120
} ,
107
- detachItem : function ( item ) {
108
- var i , node , parent ;
109
- //Don't .remove(), that will destroy element data
110
- for ( i = 0 ; i < item . element . length ; i ++ ) {
111
- node = item . element [ i ] ;
112
- parent = node . parentNode ;
113
- parent && parent . removeChild ( node ) ;
114
- }
115
- //Don't .$destroy(), just stop watchers and events firing
116
- disconnectScope ( item . scope ) ;
121
+ destroyItem : function ( item ) {
122
+ item . element . remove ( ) ;
123
+ item . scope . $destroy ( ) ;
124
+ item . scope = null ;
125
+ item . element = null ;
117
126
} ,
118
- attachItem : function ( item ) {
119
- if ( ! item . element [ 0 ] . parentNode ) {
120
- this . transcludeParent [ 0 ] . appendChild ( item . element [ 0 ] ) ;
127
+ detachItem : function ( item ) {
128
+ delete this . attachedItems [ item . hash ] ;
129
+
130
+ // If we are at the limit of backup items, just get rid of the this element
131
+ if ( this . backupItemsArray . length >= this . BACKUP_ITEMS_LENGTH ) {
132
+ this . destroyItem ( item ) ;
133
+ // Otherwise, add it to our backup items
134
+ } else {
135
+ this . backupItemsArray . push ( item ) ;
136
+ item . element . css ( ionic . CSS . TRANSFORM , 'translate3d(-2000px,-2000px,0)' ) ;
137
+ //Don't .$destroy(), just stop watchers and events firing
138
+ disconnectScope ( item . scope ) ;
121
139
}
122
- reconnectScope ( item . scope ) ;
123
- ! $rootScope . $$phase && item . scope . $digest ( ) ;
124
140
} ,
125
141
getLength : function ( ) {
126
142
return this . data && this . data . length || 0 ;
0 commit comments