@@ -7,78 +7,94 @@ export default Ember.TextField.extend({
77
88 classNames : [ 'form-control' ] ,
99
10- _setElement$ : function ( ) {
11- var element$ = Ember . $ ( '#' + this . get ( 'elementId' ) ) ;
12- this . set ( 'element$' , element$ ) ;
10+ /*
11+ Appends only truthy values that are not promises.
12+ */
13+ _appendOption : function ( options , attributeName ) {
14+ let attrValue = this . get ( attributeName ) ;
15+ if ( attrValue && typeof attrValue . then !== 'function' ) {
16+ options [ attributeName ] = attrValue ;
17+ }
18+ return options ;
1319 } ,
1420
15- tokens : null ,
16-
17- autocomplete : null ,
21+ _buildTokenfieldOptions : function ( ) {
22+ var options = { } ;
23+ options = this . _appendOption ( options , 'limit' ) ;
24+ options = this . _appendOption ( options , 'minLength' ) ;
25+ options = this . _appendOption ( options , 'minWidth' ) ;
26+ options = this . _appendOption ( options , 'showAutocompleteOnFocus' ) ;
27+ options = this . _appendOption ( options , 'delimiter' ) ;
28+ options = this . _appendOption ( options , 'beautify' ) ;
29+ options = this . _appendOption ( options , 'inputType' ) ;
30+ options = this . _appendOption ( options , 'createTokenOnBlur' ) ;
31+ options = this . _appendOption ( options , 'typeahead' ) ;
32+ options = this . _appendOption ( options , 'tokens' ) ;
33+ options = this . _appendOption ( options , 'autocomplete' ) ;
34+ return options ;
35+ } ,
1836
1937 didInsertElement : function ( ) {
20- this . _setElement$ ( ) ;
21-
22- var tokens = this . _processTokensObject ( ) ;
23- var autocomplete = this . _processAutocompleteObject ( ) ;
38+ let element$ = Ember . $ ( '#' + this . get ( 'elementId' ) ) ;
39+ this . set ( 'element$' , element$ ) ;
2440
25- var options = { } ;
26- if ( tokens ) { options . tokens = tokens ; }
27- if ( this . limit ) { options . limit = this . limit ; }
28- if ( this . minLength ) { options . minLength = this . minLength ; }
29- if ( this . minWidth ) { options . minWidth = this . minWidth ; }
30- if ( autocomplete ) { options . autocomplete = autocomplete ; }
31- if ( this . showAutocompleteOnFocus ) {
32- options . showAutocompleteOnFocus = true ;
33- }
41+ var options = this . _buildTokenfieldOptions ( ) ;
3442
35- var typeahead = this . get ( 'typeahead' ) ;
36- if ( typeahead ) { options . typeahead = typeahead ; }
37- if ( this . createTokensOnBlur ) {
38- options . createTokensOnBlur = this . createTokensOnBlur ;
39- }
40- if ( this . delimiter ) { options . delimiter = this . delimiter ; }
41- if ( this . beautify ) { options . beautify = this . beautify ; }
42- if ( this . inputType ) { options . inputType = this . inputType ; }
43+ element$ . tokenfield ( options ) ;
4344
44- this . get ( 'element$' ) . tokenfield ( options ) ;
45+ this . _consumeAutocompletePromise ( ) ;
46+ this . _consumeTokensPromise ( ) ;
4547 } ,
4648
47- /*
48- Knows how to handle the autocomplete object if its a promise.
49- */
50- _processAutocompleteObject : Ember . observer ( 'autocomplete' , function ( ) {
49+ _consumeAutocompletePromise : function ( ) {
5150 var autocomplete = this . get ( 'autocomplete' ) ;
5251
53- if ( ! autocomplete || typeof autocomplete . then !== 'function' ) {
54- return autocomplete ;
52+ if ( ! autocomplete || typeof autocomplete . then !== 'function' ) {
53+ return ; // nothing to do, value already passed in with the options object
5554 }
5655
57- var _this = this ;
58- autocomplete . then ( function ( resolvedAutocomplete ) {
59- _this . get ( 'element$' )
60- . data ( 'bs.tokenfield' )
61- . $input . autocomplete ( resolvedAutocomplete ) ;
56+ autocomplete . then ( autocompleteValues => {
57+ let element$ = this . get ( 'element$' ) ;
58+ if ( element$ ) {
59+ element$
60+ . data ( 'bs.tokenfield' )
61+ . $input . autocomplete ( autocompleteValues ) ;
62+ }
63+
64+ this . set ( 'autocomplete' , autocompleteValues ) ;
6265 } ) ;
66+ } ,
6367
64- return null ;
68+ _observeAutocomplete : Ember . observer ( 'autocomplete' , function ( ) {
69+ this . _consumeAutocompletePromise ( ) ;
6570 } ) ,
6671
67- _processTokensObject : Ember . observer ( 'tokens' , 'tokens.[]' , function ( ) {
72+ _consumeTokensPromise : function ( ) {
6873 var tokens = this . get ( 'tokens' ) ;
6974
70- if ( ! tokens ) {
71- return ;
75+ if ( ! tokens || typeof tokens . then !== 'function' ) {
76+ return ; // nothing to do, value already passed in with the options object
7277 }
7378
7479 // test for Ember.ArrayProxy
75- if ( typeof tokens . get ( 'hasArrayObservers' ) === 'boolean' ) {
76- tokens = tokens . toArray ( ) ;
80+ /*
81+ if ( tokens.get && typeof tokens.get('hasArrayObservers') === 'boolean') {
82+ let tokensList = tokens.toArray();
83+ element$.tokenfield('setTokens', tokensList);
7784 }
85+ */
86+
87+ tokens . then ( tokensList => {
88+ let element$ = this . get ( 'element$' ) ;
89+ if ( element$ ) {
90+ element$ . tokenfield ( 'setTokens' , tokensList ) ;
91+ }
92+ this . set ( 'tokens' , tokensList ) ;
93+ } ) ;
94+ } ,
7895
79- this . get ( 'element$' ) . tokenfield ( 'setTokens' , tokens ) ;
80-
81- return tokens ;
96+ _observeTokens : Ember . observer ( 'tokens' , function ( ) {
97+ this . _consumeTokensPromise ( ) ;
8298 } )
8399
84100} ) ;
0 commit comments