Skip to content

Commit b4c5634

Browse files
authored
Merge pull request #382 from jeff-phillips-18/remove-scope
Use $doCheck instead of $scope.$watch in filter, sort, and toolbar co…
2 parents b74bfc6 + 12b428d commit b4c5634

File tree

5 files changed

+80
-38
lines changed

5 files changed

+80
-38
lines changed

src/filters/filter-fields-component.js

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ angular.module('patternfly.filters').component('pfFilterFields', {
2727
addFilterFn: '<'
2828
},
2929
templateUrl: 'filters/filter-fields.html',
30-
controller: function ($scope) {
30+
controller: function () {
3131
'use strict';
3232

3333
var ctrl = this;
34+
var prevConfig;
3435

3536
ctrl.$onInit = function () {
3637
angular.extend(ctrl, {
@@ -40,42 +41,58 @@ angular.module('patternfly.filters').component('pfFilterFields', {
4041
});
4142
};
4243

43-
ctrl.$postLink = function () {
44-
$scope.$watch('config', function () {
44+
ctrl.$onChanges = function () {
45+
setupConfig ();
46+
};
47+
48+
ctrl.$doCheck = function () {
49+
// do a deep compare on config
50+
if (!angular.equals(ctrl.config, prevConfig)) {
4551
setupConfig();
46-
}, true);
52+
}
4753
};
4854

4955
function selectField (item) {
5056
ctrl.currentField = item;
51-
ctrl.config.currentValue = null;
57+
ctrl.currentValue = null;
5258
}
5359

5460
function selectValue (filterValue) {
5561
if (angular.isDefined(filterValue)) {
56-
ctrl.addFilterFn(scope.currentField, filterValue);
57-
ctrl.config.currentValue = null;
62+
ctrl.addFilterFn(ctrl.currentField, filterValue);
63+
ctrl.currentValue = null;
5864
}
5965
}
6066

6167
function onValueKeyPress (keyEvent) {
6268
if (keyEvent.which === 13) {
63-
ctrl.addFilterFn(ctrl.currentField, ctrl.config.currentValue);
64-
ctrl.config.currentValue = undefined;
69+
ctrl.addFilterFn(ctrl.currentField, ctrl.currentValue);
70+
ctrl.currentValue = undefined;
6571
}
6672
}
6773

6874
function setupConfig () {
69-
if (ctrl.fields === undefined) {
70-
ctrl.fields = [];
75+
var fieldFound = false;
76+
77+
prevConfig = angular.copy(ctrl.config);
78+
79+
if (ctrl.config.fields === undefined) {
80+
ctrl.config.fields = [];
81+
}
82+
83+
if (ctrl.currentField) {
84+
fieldFound = _.find(ctrl.config.fields, function (nextField) {
85+
return nextField.id === ctrl.currentField.id;
86+
});
7187
}
72-
if (!ctrl.currentField) {
88+
89+
if (!fieldFound) {
7390
ctrl.currentField = ctrl.config.fields[0];
74-
ctrl.config.currentValue = null;
91+
ctrl.currentValue = null;
7592
}
7693

77-
if (ctrl.config.currentValue === undefined) {
78-
ctrl.config.currentValue = null;
94+
if (ctrl.currentValue === undefined) {
95+
ctrl.currentValue = null;
7996
}
8097
}
8198
}

src/filters/filter-fields.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
</ul>
1515
</div>
1616
<div ng-if="$ctrl.currentField.filterType !== 'select'">
17-
<input class="form-control" type="{{$ctrl.currentField.filterType}}" ng-model="$ctrl.config.currentValue"
17+
<input class="form-control" type="{{$ctrl.currentField.filterType}}" ng-model="$ctrl.currentValue"
1818
placeholder="{{$ctrl.currentField.placeholder}}"
1919
ng-keypress="$ctrl.onValueKeyPress($event)"/>
2020
</div>
2121
<div ng-if="$ctrl.currentField.filterType === 'select'">
2222
<div class="btn-group bootstrap-select form-control filter-select" uib-dropdown >
2323
<button type="button" uib-dropdown-toggle class="btn btn-default dropdown-toggle">
24-
<span class="filter-option pull-left">{{$ctrl.config.currentValue || $ctrl.currentField.placeholder}}</span>
24+
<span class="filter-option pull-left">{{$ctrl.currentValue || $ctrl.currentField.placeholder}}</span>
2525
<span class="caret"></span>
2626
</button>
2727
<ul uib-dropdown-menu class="dropdown-menu-right" role="menu">
@@ -30,7 +30,7 @@
3030
{{$ctrl.currentField.placeholder}}
3131
</a>
3232
</li>
33-
<li ng-repeat="filterValue in $ctrl.currentField.filterValues" ng-class="{'selected': filterValue === $ctrl.config.currentValue}">
33+
<li ng-repeat="filterValue in $ctrl.currentField.filterValues" ng-class="{'selected': filterValue === $ctrl.currentValue}">
3434
<a role="menuitem" tabindex="-1" ng-click="$ctrl.selectValue(filterValue)">
3535
{{filterValue}}
3636
</a>

src/filters/filter-results-component.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ angular.module('patternfly.filters').component('pfFilterResults', {
2828
config: '='
2929
},
3030
templateUrl: 'filters/filter-results.html',
31-
controller: function ($scope) {
31+
controller: function () {
3232
'use strict';
3333

3434
var ctrl = this;
35+
var prevConfig;
3536

3637
ctrl.$onInit = function () {
3738
angular.extend(ctrl, {
@@ -40,13 +41,20 @@ angular.module('patternfly.filters').component('pfFilterResults', {
4041
});
4142
};
4243

43-
ctrl.$postLink = function () {
44-
$scope.$watch('config', function () {
45-
setupConfig();
46-
}, true);
44+
ctrl.$onChanges = function () {
45+
setupConfig ();
46+
};
47+
48+
ctrl.$doCheck = function () {
49+
// do a deep compare on config
50+
if (!angular.equals(ctrl.config, prevConfig)) {
51+
// setupConfig();
52+
}
4753
};
4854

4955
function setupConfig () {
56+
prevConfig = angular.copy(ctrl.config);
57+
5058
if (!ctrl.config.appliedFilters) {
5159
ctrl.config.appliedFilters = [];
5260
}

src/sort/sort-component.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,36 @@ angular.module('patternfly.sort').component('pfSort', {
33
config: '='
44
},
55
templateUrl: 'sort/sort.html',
6-
controller: function ($scope) {
6+
controller: function () {
77
'use strict';
88

99
var ctrl = this;
10+
var prevConfig;
1011

1112
ctrl.$onInit = function () {
1213
angular.extend(ctrl, {
1314
selectField: selectField,
1415
changeDirection: changeDirection,
1516
getSortIconClass: getSortIconClass
1617
});
18+
};
1719

20+
ctrl.$onChanges = function () {
1821
setupConfig();
19-
2022
};
2123

22-
ctrl.$postLink = function () {
23-
$scope.$watch('config', function () {
24+
ctrl.$doCheck = function () {
25+
// do a deep compare on config
26+
if (!angular.equals(ctrl.config, prevConfig)) {
2427
setupConfig();
25-
}, true);
28+
}
2629
};
2730

2831
function setupConfig () {
2932
var updated = false;
3033

34+
prevConfig = angular.copy(ctrl.config);
35+
3136
if (ctrl.config.fields === undefined) {
3237
ctrl.config.fields = [];
3338
}

src/toolbars/toolbar-component.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ angular.module('patternfly.toolbars').component('pfToolbar', {
66
'actions': '?'
77
},
88
templateUrl: 'toolbars/toolbar.html',
9-
controller: function ($scope) {
9+
controller: function () {
1010
'use strict';
1111

1212
var ctrl = this;
13+
var prevConfig;
1314

1415
ctrl.$onInit = function () {
1516
angular.extend(ctrl, {
@@ -21,18 +22,29 @@ angular.module('patternfly.toolbars').component('pfToolbar', {
2122
});
2223
};
2324

24-
ctrl.$postLink = function () {
25-
$scope.$watch('config', function () {
26-
if (ctrl.config && ctrl.config.viewsConfig && ctrl.config.viewsConfig.views) {
27-
ctrl.config.viewsConfig.viewsList = angular.copy(ctrl.config.viewsConfig.views);
25+
ctrl.$onChanges = function () {
26+
setupConfig ();
27+
};
2828

29-
if (!ctrl.config.viewsConfig.currentView) {
30-
ctrl.config.viewsConfig.currentView = ctrl.config.viewsConfig.viewsList[0];
31-
}
32-
}
33-
}, true);
29+
ctrl.$doCheck = function () {
30+
// do a deep compare on config
31+
if (!angular.equals(ctrl.config, prevConfig)) {
32+
setupConfig();
33+
}
3434
};
3535

36+
function setupConfig () {
37+
prevConfig = angular.copy(ctrl.config);
38+
39+
if (ctrl.config && ctrl.config.viewsConfig && ctrl.config.viewsConfig.views) {
40+
ctrl.config.viewsConfig.viewsList = angular.copy(ctrl.config.viewsConfig.views);
41+
42+
if (!ctrl.config.viewsConfig.currentView) {
43+
ctrl.config.viewsConfig.currentView = ctrl.config.viewsConfig.viewsList[0];
44+
}
45+
}
46+
}
47+
3648
function viewSelected (viewId) {
3749
ctrl.config.viewsConfig.currentView = viewId;
3850
if (ctrl.config.viewsConfig.onViewSelect && !ctrl.checkViewDisabled(viewId)) {

0 commit comments

Comments
 (0)