2
2
'use strict' ;
3
3
4
4
angular . module ( 'ui.grid' ) . controller ( 'uiGridController' , [ '$scope' , '$element' , '$attrs' , 'gridUtil' , '$q' , 'uiGridConstants' ,
5
- '$templateCache' , ' gridClassFactory' , '$timeout ', '$parse' , '$compile' ,
5
+ 'gridClassFactory' , '$parse' , '$compile' ,
6
6
function ( $scope , $elm , $attrs , gridUtil , $q , uiGridConstants ,
7
- $templateCache , gridClassFactory , $timeout , $parse , $compile ) {
7
+ gridClassFactory , $parse , $compile ) {
8
8
// gridUtil.logDebug('ui-grid controller');
9
-
10
9
var self = this ;
10
+ var deregFunctions = [ ] ;
11
11
12
12
self . grid = gridClassFactory . createGrid ( $scope . uiGrid ) ;
13
13
24
24
$scope . grid = self . grid ;
25
25
26
26
if ( $attrs . uiGridColumns ) {
27
- $attrs . $observe ( 'uiGridColumns' , function ( value ) {
28
- self . grid . options . columnDefs = value ;
27
+ deregFunctions . push ( $attrs . $observe ( 'uiGridColumns' , function ( value ) {
28
+ self . grid . options . columnDefs = angular . isString ( value ) ? angular . fromJson ( value ) : value ;
29
29
self . grid . buildColumns ( )
30
30
. then ( function ( ) {
31
31
self . grid . preCompileCellTemplates ( ) ;
32
32
33
33
self . grid . refreshCanvas ( true ) ;
34
34
} ) . catch ( angular . noop ) ;
35
- } ) ;
35
+ } ) ) ;
36
36
}
37
37
38
+ // prevents an error from being thrown when the array is not defined yet and fastWatch is on
39
+ function getSize ( array ) {
40
+ return array ? array . length : 0 ;
41
+ }
38
42
39
43
// if fastWatch is set we watch only the length and the reference, not every individual object
40
- var deregFunctions = [ ] ;
41
44
if ( self . grid . options . fastWatch ) {
42
45
self . uiGrid = $scope . uiGrid ;
43
46
if ( angular . isString ( $scope . uiGrid . data ) ) {
51
54
} , dataWatchFunction ) ) ;
52
55
} else {
53
56
deregFunctions . push ( $scope . $parent . $watch ( function ( ) { return $scope . uiGrid . data ; } , dataWatchFunction ) ) ;
54
- deregFunctions . push ( $scope . $parent . $watch ( function ( ) { return $scope . uiGrid . data . length ; } , function ( ) { dataWatchFunction ( $scope . uiGrid . data ) ; } ) ) ;
57
+ deregFunctions . push ( $scope . $parent . $watch ( function ( ) { return getSize ( $scope . uiGrid . data ) ; } , function ( ) { dataWatchFunction ( $scope . uiGrid . data ) ; } ) ) ;
55
58
}
56
59
deregFunctions . push ( $scope . $parent . $watch ( function ( ) { return $scope . uiGrid . columnDefs ; } , columnDefsWatchFunction ) ) ;
57
- deregFunctions . push ( $scope . $parent . $watch ( function ( ) { return $scope . uiGrid . columnDefs . length ; } , function ( ) { columnDefsWatchFunction ( $scope . uiGrid . columnDefs ) ; } ) ) ;
60
+ deregFunctions . push ( $scope . $parent . $watch ( function ( ) { return getSize ( $scope . uiGrid . columnDefs ) ; } , function ( ) { columnDefsWatchFunction ( $scope . uiGrid . columnDefs ) ; } ) ) ;
58
61
} else {
59
62
if ( angular . isString ( $scope . uiGrid . data ) ) {
60
63
deregFunctions . push ( $scope . $parent . $watchCollection ( $scope . uiGrid . data , dataWatchFunction ) ) ;
84
87
// gridUtil.logDebug('dataWatch fired');
85
88
var promises = [ ] ;
86
89
87
- if ( self . grid . options . fastWatch ) {
88
- if ( angular . isString ( $scope . uiGrid . data ) ) {
89
- newData = self . grid . appScope [ $scope . uiGrid . data ] ;
90
- } else {
91
- newData = $scope . uiGrid . data ;
92
- }
90
+ if ( angular . isString ( $scope . uiGrid . data ) ) {
91
+ newData = self . grid . appScope [ $scope . uiGrid . data ] ;
92
+ } else {
93
+ newData = $scope . uiGrid . data ;
93
94
}
94
95
95
96
mostRecentData = newData ;
148
149
} ) ;
149
150
150
151
self . fireEvent = function ( eventName , args ) {
151
- // Add the grid to the event arguments if it's not there
152
- if ( typeof ( args ) === 'undefined' || args === undefined ) {
153
- args = { } ;
154
- }
152
+ args = args || { } ;
155
153
156
- if ( typeof ( args . grid ) === 'undefined' || args . grid === undefined ) {
154
+ // Add the grid to the event arguments if it's not there
155
+ if ( angular . isUndefined ( args . grid ) ) {
157
156
args . grid = self . grid ;
158
157
}
159
158
163
162
self . innerCompile = function innerCompile ( elm ) {
164
163
$compile ( elm ) ( $scope ) ;
165
164
} ;
166
-
167
165
} ] ) ;
168
166
169
167
/**
196
194
*/
197
195
angular . module ( 'ui.grid' ) . directive ( 'uiGrid' , uiGridDirective ) ;
198
196
199
- uiGridDirective . $inject = [ '$compile' , '$templateCache' , '$timeout' , '$ window', 'gridUtil' , 'uiGridConstants' ] ;
200
- function uiGridDirective ( $compile , $templateCache , $timeout , $ window, gridUtil , uiGridConstants ) {
197
+ uiGridDirective . $inject = [ '$window' , 'gridUtil' , 'uiGridConstants' ] ;
198
+ function uiGridDirective ( $window , gridUtil , uiGridConstants ) {
201
199
return {
202
200
templateUrl : 'ui-grid/ui-grid' ,
203
201
scope : {
@@ -239,32 +237,36 @@ function uiGridDirective($compile, $templateCache, $timeout, $window, gridUtil,
239
237
if ( $elm [ 0 ] . offsetWidth <= 0 && sizeChecks < maxSizeChecks ) {
240
238
setTimeout ( checkSize , sizeCheckInterval ) ;
241
239
sizeChecks ++ ;
242
- }
243
- else {
244
- $timeout ( init ) ;
240
+ } else {
241
+ $scope . $applyAsync ( init ) ;
245
242
}
246
243
}
247
244
248
245
// Setup event listeners and watchers
249
246
function setup ( ) {
247
+ var deregisterLeftWatcher ;
248
+ var deregisterRightWatcher ;
249
+
250
250
// Bind to window resize events
251
251
angular . element ( $window ) . on ( 'resize' , gridResize ) ;
252
252
253
253
// Unbind from window resize events when the grid is destroyed
254
254
$elm . on ( '$destroy' , function ( ) {
255
255
angular . element ( $window ) . off ( 'resize' , gridResize ) ;
256
+ deregisterLeftWatcher ( ) ;
257
+ deregisterRightWatcher ( ) ;
256
258
} ) ;
257
259
258
260
// If we add a left container after render, we need to watch and react
259
- $scope . $watch ( function ( ) { return grid . hasLeftContainer ( ) ; } , function ( newValue , oldValue ) {
261
+ deregisterLeftWatcher = $scope . $watch ( function ( ) { return grid . hasLeftContainer ( ) ; } , function ( newValue , oldValue ) {
260
262
if ( newValue === oldValue ) {
261
263
return ;
262
264
}
263
265
grid . refreshCanvas ( true ) ;
264
266
} ) ;
265
267
266
268
// If we add a right container after render, we need to watch and react
267
- $scope . $watch ( function ( ) { return grid . hasRightContainer ( ) ; } , function ( newValue , oldValue ) {
269
+ deregisterRightWatcher = $scope . $watch ( function ( ) { return grid . hasRightContainer ( ) ; } , function ( newValue , oldValue ) {
268
270
if ( newValue === oldValue ) {
269
271
return ;
270
272
}
@@ -337,7 +339,7 @@ function uiGridDirective($compile, $templateCache, $timeout, $window, gridUtil,
337
339
}
338
340
339
341
// Resize the grid on window resize events
340
- function gridResize ( $event ) {
342
+ function gridResize ( ) {
341
343
grid . gridWidth = $scope . gridWidth = gridUtil . elementWidth ( $elm ) ;
342
344
grid . gridHeight = $scope . gridHeight = gridUtil . elementHeight ( $elm ) ;
343
345
@@ -348,5 +350,4 @@ function uiGridDirective($compile, $templateCache, $timeout, $window, gridUtil,
348
350
}
349
351
} ;
350
352
}
351
-
352
353
} ) ( ) ;
0 commit comments