|
19 | 19 | require: 'uiGrid',
|
20 | 20 | scope: false,
|
21 | 21 | link: function($scope, $elm, $attrs, uiGridCtrl) {
|
| 22 | + var timeout = null; |
22 | 23 |
|
23 |
| - $scope.$watch(function() { |
24 |
| - return $elm[0].clientWidth; |
25 |
| - }, function(newVal, oldVal) { |
26 |
| - if (newVal !== oldVal) { |
27 |
| - uiGridCtrl.grid.gridWidth = newVal; |
28 |
| - uiGridCtrl.grid.refresh(); |
| 24 | + var debounce = function(width, height) { |
| 25 | + if (timeout !== null) { |
| 26 | + clearTimeout(timeout); |
29 | 27 | }
|
30 |
| - }); |
31 |
| - |
32 |
| - $scope.$watch(function() { |
33 |
| - return $elm[0].clientHeight; |
34 |
| - }, function(newVal, oldVal) { |
35 |
| - if (newVal !== oldVal) { |
36 |
| - uiGridCtrl.grid.gridHeight = newVal; |
| 28 | + timeout = setTimeout(function() { |
| 29 | + uiGridCtrl.grid.gridWidth = width; |
| 30 | + uiGridCtrl.grid.gridHeight = height; |
37 | 31 | uiGridCtrl.grid.refresh();
|
| 32 | + timeout = null; |
| 33 | + }, 400); |
| 34 | + }; |
| 35 | + |
| 36 | + $scope.$watchGroup([ |
| 37 | + function() { |
| 38 | + return gridUtil.elementWidth($elm); |
| 39 | + }, |
| 40 | + function() { |
| 41 | + return gridUtil.elementHeight($elm); |
| 42 | + } |
| 43 | + ], function(newValues, oldValues, scope) { |
| 44 | + if (!angular.equals(newValues, oldValues)) { |
| 45 | + debounce(newValues[0], newValues[1]); |
38 | 46 | }
|
39 | 47 | });
|
40 |
| - |
41 | 48 | }
|
42 | 49 | };
|
43 | 50 | }]);
|
|
0 commit comments