@@ -13,8 +13,7 @@ angular.module('dev-server', ['angularjs-input-tags'])
1313 disabled="$ctrl.disabled"
1414 key-property="code"
1515 display-property="title"
16- input-search="$ctrl.inputSearch"
17- get-suggestions="$ctrl.getSuggestions"
16+ get-suggestions="$ctrl.updateSuggestions"
1817 min-length="1"></input-tags>` ,
1918 controller : searchCtrl
2019 } ) ;
@@ -23,22 +22,31 @@ function searchCtrl() {
2322 const vm = this ;
2423
2524 vm . $onInit = ( ) => {
26- vm . inputSearch = '' ;
2725 vm . tags = [ ] ;
2826 vm . disabled = false ;
2927 vm . suggestions = { title : '' , data : [ ] } ;
30- vm . getSuggestions ( ) ;
28+ vm . updateSuggestions ( ) ;
3129 } ;
3230
33- function flat ( r , a ) {
34- r . push ( a ) ;
35- if ( Array . isArray ( a . data ) ) {
36- return a . data . reduce ( flat , r ) ;
31+ function flat ( accumulator , currentValue ) {
32+ accumulator . push ( currentValue ) ;
33+ if ( Array . isArray ( currentValue . data ) ) {
34+ return currentValue . data . reduce ( flat , accumulator ) ;
3735 }
38- return r ;
36+ return accumulator ;
3937 }
4038
41- vm . getSuggestions = search => {
39+ vm . updateSuggestions = search => {
40+ const newSuggestions = searchSuggestions ( search ) ;
41+
42+ vm . suggestions . data . length = 0 ;
43+ vm . suggestions . title = newSuggestions . title ;
44+ Array . prototype . push . apply ( vm . suggestions . data , newSuggestions . data ) ;
45+
46+ console . log ( 'devServer:getSuggestions.suggestions' , vm . suggestions ) ;
47+ } ;
48+
49+ function searchSuggestions ( search ) {
4250 const mock = {
4351 title : 'root' ,
4452 data : [
@@ -93,14 +101,11 @@ function searchCtrl() {
93101 ]
94102 } ;
95103
96- const result = search ? mock . data . reduce ( flat , [ ] ) . filter ( elem => {
97- return String ( elem . title ) . indexOf ( String ( search ) ) >= 0 ;
98- } ) : mock . data ;
99-
100- vm . suggestions . data . length = 0 ;
101- vm . suggestions . title = mock . title ;
102- Array . prototype . push . apply ( vm . suggestions . data , result ) ;
103-
104- console . log ( 'devServer:getSuggestions.suggestions' , vm . suggestions ) ;
105- } ;
104+ return {
105+ title : mock . title ,
106+ data : ( search ? mock . data . reduce ( flat , [ ] ) . filter ( elem => {
107+ return String ( elem . title ) . indexOf ( String ( search ) ) >= 0 ;
108+ } ) : mock . data )
109+ } ;
110+ }
106111}
0 commit comments