@@ -4,52 +4,54 @@ angular.module('angular-bootstrap-select.extra', [])
4
4
return {
5
5
restrict : 'A' ,
6
6
link : function ( scope , element , attrs ) {
7
- //prevent directive from attaching itself to everything that defines a toggle attribute
7
+ // prevent directive from attaching itself to everything that defines a toggle attribute
8
8
if ( ! element . hasClass ( 'selectpicker' ) ) {
9
9
return ;
10
10
}
11
11
12
12
var target = element . parent ( ) ;
13
+ var toggleFn = function ( ) {
14
+ target . toggleClass ( 'open' ) ;
15
+ } ;
16
+ var hideFn = function ( ) {
17
+ target . removeClass ( 'open' ) ;
18
+ } ;
13
19
14
- element . bind ( 'click' , function ( ) {
15
- target . toggleClass ( 'open' )
16
- } ) ;
20
+ element . on ( 'click' , toggleFn ) ;
21
+ element . next ( ) . on ( 'click' , hideFn ) ;
17
22
18
- element . next ( ) . find ( 'li' ) . bind ( 'click' , function ( ) {
19
- target . toggleClass ( 'open' )
20
- } )
23
+ scope . $on ( '$destroy' , function ( ) {
24
+ element . off ( 'click' , toggleFn ) ;
25
+ element . next ( ) . off ( 'click' , hideFn ) ;
26
+ } ) ;
21
27
}
22
28
} ;
23
29
} ) ;
24
30
25
31
angular . module ( 'angular-bootstrap-select' , [ ] )
26
- . directive ( 'selectpicker' , [ '$timeout' , '$ parse', function ( $timeout , $parse ) {
32
+ . directive ( 'selectpicker' , [ '$parse' , function ( $parse ) {
27
33
return {
28
34
restrict : 'A' ,
29
35
require : '?ngModel' ,
30
- priority : 1001 ,
36
+ priority : 10 ,
31
37
compile : function ( tElement , tAttrs , transclude ) {
32
38
tElement . selectpicker ( $parse ( tAttrs . selectpicker ) ( ) ) ;
39
+ tElement . selectpicker ( 'refresh' ) ;
33
40
return function ( scope , element , attrs , ngModel ) {
34
- if ( angular . isUndefined ( ngModel ) ) {
35
- return ;
36
- }
41
+ if ( ! ngModel ) return ;
37
42
38
- scope . $watch ( attrs . ngModel , function ( ) {
39
- $timeout ( function ( ) {
40
- element . selectpicker ( 'val' , element . val ( ) ) ;
43
+ scope . $watch ( attrs . ngModel , function ( newVal , oldVal ) {
44
+ scope . $evalAsync ( function ( ) {
45
+ if ( ! attrs . ngOptions || / t r a c k b y / . test ( attrs . ngOptions ) ) element . val ( newVal ) ;
41
46
element . selectpicker ( 'refresh' ) ;
42
47
} ) ;
43
48
} ) ;
44
49
45
50
ngModel . $render = function ( ) {
46
- $timeout ( function ( ) {
47
- element . selectpicker ( 'val' , ngModel . $viewValue || '' ) ;
51
+ scope . $evalAsync ( function ( ) {
48
52
element . selectpicker ( 'refresh' ) ;
49
53
} ) ;
50
- } ;
51
-
52
- ngModel . $viewValue = element . val ( ) ;
54
+ }
53
55
} ;
54
56
}
55
57
0 commit comments