Skip to content

Commit 56f0dc1

Browse files
Portugal, Marcelomportuga
Portugal, Marcelo
authored andcommitted
fix(uiGridDirective): Reducing digest cycles and improving coverage.
Refactoring for better performance.
1 parent 1f116fe commit 56f0dc1

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

Diff for: src/js/core/directives/ui-grid.js

+31-30
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
'use strict';
33

44
angular.module('ui.grid').controller('uiGridController', ['$scope', '$element', '$attrs', 'gridUtil', '$q', 'uiGridConstants',
5-
'$templateCache', 'gridClassFactory', '$timeout', '$parse', '$compile',
5+
'gridClassFactory', '$parse', '$compile',
66
function ($scope, $elm, $attrs, gridUtil, $q, uiGridConstants,
7-
$templateCache, gridClassFactory, $timeout, $parse, $compile) {
7+
gridClassFactory, $parse, $compile) {
88
// gridUtil.logDebug('ui-grid controller');
9-
109
var self = this;
10+
var deregFunctions = [];
1111

1212
self.grid = gridClassFactory.createGrid($scope.uiGrid);
1313

@@ -24,20 +24,23 @@
2424
$scope.grid = self.grid;
2525

2626
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;
2929
self.grid.buildColumns()
3030
.then(function(){
3131
self.grid.preCompileCellTemplates();
3232

3333
self.grid.refreshCanvas(true);
3434
}).catch(angular.noop);
35-
});
35+
}) );
3636
}
3737

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+
}
3842

3943
// if fastWatch is set we watch only the length and the reference, not every individual object
40-
var deregFunctions = [];
4144
if (self.grid.options.fastWatch) {
4245
self.uiGrid = $scope.uiGrid;
4346
if (angular.isString($scope.uiGrid.data)) {
@@ -51,10 +54,10 @@
5154
}, dataWatchFunction) );
5255
} else {
5356
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); }) );
5558
}
5659
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); }) );
5861
} else {
5962
if (angular.isString($scope.uiGrid.data)) {
6063
deregFunctions.push( $scope.$parent.$watchCollection($scope.uiGrid.data, dataWatchFunction) );
@@ -84,12 +87,10 @@
8487
// gridUtil.logDebug('dataWatch fired');
8588
var promises = [];
8689

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;
9394
}
9495

9596
mostRecentData = newData;
@@ -148,12 +149,10 @@
148149
});
149150

150151
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 || {};
155153

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)) {
157156
args.grid = self.grid;
158157
}
159158

@@ -163,7 +162,6 @@
163162
self.innerCompile = function innerCompile(elm) {
164163
$compile(elm)($scope);
165164
};
166-
167165
}]);
168166

169167
/**
@@ -196,8 +194,8 @@
196194
*/
197195
angular.module('ui.grid').directive('uiGrid', uiGridDirective);
198196

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) {
201199
return {
202200
templateUrl: 'ui-grid/ui-grid',
203201
scope: {
@@ -239,32 +237,36 @@ function uiGridDirective($compile, $templateCache, $timeout, $window, gridUtil,
239237
if ($elm[0].offsetWidth <= 0 && sizeChecks < maxSizeChecks) {
240238
setTimeout(checkSize, sizeCheckInterval);
241239
sizeChecks++;
242-
}
243-
else {
244-
$timeout(init);
240+
} else {
241+
$scope.$applyAsync(init);
245242
}
246243
}
247244

248245
// Setup event listeners and watchers
249246
function setup() {
247+
var deregisterLeftWatcher;
248+
var deregisterRightWatcher;
249+
250250
// Bind to window resize events
251251
angular.element($window).on('resize', gridResize);
252252

253253
// Unbind from window resize events when the grid is destroyed
254254
$elm.on('$destroy', function () {
255255
angular.element($window).off('resize', gridResize);
256+
deregisterLeftWatcher();
257+
deregisterRightWatcher();
256258
});
257259

258260
// 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) {
260262
if (newValue === oldValue) {
261263
return;
262264
}
263265
grid.refreshCanvas(true);
264266
});
265267

266268
// 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) {
268270
if (newValue === oldValue) {
269271
return;
270272
}
@@ -337,7 +339,7 @@ function uiGridDirective($compile, $templateCache, $timeout, $window, gridUtil,
337339
}
338340

339341
// Resize the grid on window resize events
340-
function gridResize($event) {
342+
function gridResize() {
341343
grid.gridWidth = $scope.gridWidth = gridUtil.elementWidth($elm);
342344
grid.gridHeight = $scope.gridHeight = gridUtil.elementHeight($elm);
343345

@@ -348,5 +350,4 @@ function uiGridDirective($compile, $templateCache, $timeout, $window, gridUtil,
348350
}
349351
};
350352
}
351-
352353
})();

0 commit comments

Comments
 (0)