Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit c806e8b

Browse files
author
Robert Messerle
committed
feat(tabs): replaces unnecessary watches with getter/setter syntax for improved performance
1 parent 761ded6 commit c806e8b

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/components/tabs/js/tabsController.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@ angular
77
*/
88
function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $mdTabInkRipple,
99
$mdUtil, $animate) {
10-
var ctrl = this,
11-
locked = false,
12-
elements = getElements(),
13-
queue = [],
14-
destroyed = false,
15-
loaded = false;
10+
var ctrl = this,
11+
locked = false,
12+
elements = getElements(),
13+
queue = [],
14+
destroyed = false,
15+
loaded = false;
16+
17+
defineProperty('focusIndex', handleFocusIndexChange, $scope.selectedIndex || 0);
18+
defineProperty('offsetLeft', handleOffsetChange, 0);
19+
defineProperty('hasContent', handleHasContent, false);
1620

1721
ctrl.scope = $scope;
1822
ctrl.parent = $scope.$parent;
1923
ctrl.tabs = [];
2024
ctrl.lastSelectedIndex = null;
21-
ctrl.focusIndex = $scope.selectedIndex || 0;
22-
ctrl.offsetLeft = 0;
23-
ctrl.hasContent = false;
2425
ctrl.hasFocus = false;
2526
ctrl.lastClick = true;
2627
ctrl.shouldPaginate = false;
@@ -47,9 +48,6 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md
4748

4849
function init () {
4950
$scope.$watch('selectedIndex', handleSelectedIndexChange);
50-
$scope.$watch('$mdTabsCtrl.focusIndex', handleFocusIndexChange);
51-
$scope.$watch('$mdTabsCtrl.offsetLeft', handleOffsetChange);
52-
$scope.$watch('$mdTabsCtrl.hasContent', handleHasContent);
5351
angular.element($window).on('resize', handleWindowResize);
5452
angular.element(elements.paging).on('DOMSubtreeModified', ctrl.updateInkBarStyles);
5553
angular.element(elements.paging).on('DOMSubtreeModified', updatePagination);
@@ -286,6 +284,17 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md
286284

287285
//-- Utility methods
288286

287+
function defineProperty (key, handler, value) {
288+
Object.defineProperty(ctrl, key, {
289+
get: function () { return value; },
290+
set: function (newValue) {
291+
var oldValue = value;
292+
value = newValue;
293+
handler(newValue, oldValue);
294+
}
295+
});
296+
}
297+
289298
function updatePagination () {
290299
ctrl.shouldPaginate = shouldPaginate();
291300
ctrl.shouldCenterTabs = shouldCenterTabs();

0 commit comments

Comments
 (0)