Skip to content

Commit f654a01

Browse files
committed
added track by
1 parent 2bc4e71 commit f654a01

File tree

4 files changed

+60
-26
lines changed

4 files changed

+60
-26
lines changed

Diff for: demo/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ <h1>Angular bootstrap-select</h1>
6262
<option value="">Select one</option>
6363
</select>
6464

65-
<select name="field4" toggle ng-model="formData.color.name" selectpicker class="" ng-options="color.name as color.name for color in colors">
65+
<select name="field4" toggle ng-model="formData.color.name" selectpicker class="" ng-options="color.name as color.name for color in colors track by color.name">
6666
<option value="">Select one</option>
6767
</select>
6868

Diff for: demo/selectDemo.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function SelectCtrl($scope, $timeout) {
44
$scope.formData = {};
55
$scope.formData.color = { name: 'Green' };
66

7-
$scope.colors = [{ name: 'Red' }, { name: 'Green' }, { name: 'Blue' }];
7+
$scope.colors = [{ id: 1, name: 'Red' }, { id: 2, name: 'Green' }, { id: 3, name: 'Blue' }];
88

99
$scope.submit = function () {
1010
$scope.form1.$setPristine();

Diff for: src/angular-bootstrap-select.js

+12-16
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,48 @@ angular.module('angular-bootstrap-select.extra', [])
1010
}
1111

1212
var target = element.parent();
13-
var toggleFn = function (e) {
13+
var toggleFn = function () {
1414
target.toggleClass('open');
15-
e.stopPropagation();
15+
};
16+
var hideFn = function () {
17+
target.removeClass('open');
1618
};
1719

18-
element.bind('click', toggleFn);
19-
element.next().find('li').bind('click', toggleFn);
20+
element.on('click', toggleFn);
21+
element.next().on('click', hideFn);
2022

2123
scope.$on('$destroy', function () {
22-
console.log('lalal');
23-
element.unbind('click', toggleFn);
24+
element.off('click', toggleFn);
25+
element.next().off('click', hideFn);
2426
});
2527
}
2628
};
2729
});
2830

2931
angular.module('angular-bootstrap-select', [])
30-
.directive('selectpicker', ['$timeout', '$parse', function ($timeout, $parse) {
32+
.directive('selectpicker', ['$parse', function ($parse) {
3133
return {
3234
restrict: 'A',
3335
require: '?ngModel',
3436
priority: 10,
3537
compile: function (tElement, tAttrs, transclude) {
3638
tElement.selectpicker($parse(tAttrs.selectpicker)());
37-
// tElement.selectpicker('refresh');
39+
tElement.selectpicker('refresh');
3840
return function (scope, element, attrs, ngModel) {
3941
if (!ngModel) return;
4042

4143
scope.$watch(attrs.ngModel, function (newVal, oldVal) {
4244
scope.$evalAsync(function () {
43-
if (newVal !== oldVal) {
44-
console.log('watch --->', newVal, oldVal)
45-
if (!attrs.ngOptions) element.val(newVal);
46-
element.selectpicker('refresh');
47-
}
45+
if (!attrs.ngOptions || /track by/.test(attrs.ngOptions)) element.val(newVal);
46+
element.selectpicker('refresh');
4847
});
4948
});
5049

5150
ngModel.$render = function () {
5251
scope.$evalAsync(function () {
53-
console.log('render --->', element.val());
5452
element.selectpicker('refresh');
5553
});
5654
}
57-
58-
// element.selectpicker('refresh');
5955
};
6056
}
6157

Diff for: test/select.js

+46-8
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,72 @@ describe('selectpicker', function() {
1515
});
1616

1717
it('should set to selectpicker element value the scope value', function() {
18-
return inject(function ($compile, $rootScope, $timeout) {
18+
return inject(function ($compile, $rootScope) {
1919
var scope = $rootScope.$new();
2020
var html = '<select selectpicker ng-model="selected" class="">' +
2121
'<option>Mustard</option>' +
2222
'<option>Ketchup</option>' +
2323
'<option>Relish</option>' +
2424
'</select>';
2525
var element = $compile(html)(scope);
26+
scope.$digest();
2627
scope.$apply(function () {
2728
scope.selected = 'Ketchup';
2829
});
29-
$timeout.flush();
3030
expect(element.val()).toEqual('Ketchup');
3131
});
3232
});
3333

34-
it('should set selectpicker select element options with ng-options', function() {
35-
return inject(function ($compile, $rootScope, $timeout) {
34+
it('should set selectpicker selected option element, changing the model value', function() {
35+
return inject(function ($compile, $rootScope) {
3636
var scope = $rootScope.$new();
37-
scope.colors = [{ name: 'Red' }, { name: 'Green' }, { name: 'Blue' }];
38-
var html = '<select selectpicker ng-model="selected" ng-options="color.name for color in colors"></select>';
37+
scope.colors = [{ id: 10, name: 'Red' }, { id: 20, name: 'Green' }, { id: 30, name: 'Blue' }];
38+
var html = '<select selectpicker ng-model="selected" ng-options="c.name as c.name for c in colors"></select>';
3939
var element = $compile(html)(scope);
40+
scope.$digest();
4041
scope.$apply(function () {
41-
scope.selected = 1;
42+
scope.selected = 'Green';
4243
});
43-
$timeout.flush();
4444
expect(element.val()).toEqual('1');
4545
});
4646
});
47+
48+
it('should set selectpicker selected option element, changing the model value with options tracked by name', function() {
49+
return inject(function ($compile, $rootScope) {
50+
var scope = $rootScope.$new();
51+
scope.colors = [{ id: 10, name: 'Red' }, { id: 20, name: 'Green' }, { id: 30, name: 'Blue' }];
52+
var html = '<select selectpicker ng-model="selected" ng-options="c.name as c.name for c in colors track by c.name"></select>';
53+
var element = $compile(html)(scope);
54+
scope.$digest();
55+
scope.$apply(function () {
56+
scope.selected = 'Green';
57+
});
58+
expect(element.val()).toEqual('Green');
59+
});
60+
});
61+
62+
it('should set model value, changing selectpicker selected element tracked by id', function() {
63+
return inject(function ($compile, $rootScope) {
64+
var scope = $rootScope.$new();
65+
scope.colors = [{ id: 10, name: 'Red' }, { id: 20, name: 'Green' }, { id: 30, name: 'Blue' }];
66+
var html = '<select selectpicker ng-model="selected" ng-options="c.name as c.name for c in colors track by c.id"></select>';
67+
var element = $compile(html)(scope);
68+
scope.$digest();
69+
element.selectpicker('val', '20');
70+
expect(scope.selected).toEqual('Green');
71+
});
72+
});
73+
74+
it('should set model value, changing selectpicker selected element tracked by name', function() {
75+
return inject(function ($compile, $rootScope) {
76+
var scope = $rootScope.$new();
77+
scope.colors = [{ id: 10, name: 'Red' }, { id: 20, name: 'Green' }, { id: 30, name: 'Blue' }];
78+
var html = '<select selectpicker ng-model="selected" ng-options="c.name as c.name for c in colors track by c.name"></select>';
79+
var element = $compile(html)(scope);
80+
scope.$digest();
81+
element.selectpicker('val', 'Green');
82+
expect(scope.selected).toEqual('Green');
83+
});
84+
});
4785

4886
});

0 commit comments

Comments
 (0)