@@ -71,27 +71,34 @@ class SearchCodeModel extends BaseModel {
7171 const langParams = lang . length ? ( '&lan=' + lang . join ( ',' ) . split ( ',' ) . join ( '&lan=' ) ) : '' ;
7272 const qParams = q . replace ( ' ' , '+' ) ;
7373 const url = `//searchcode.com/api/jsonp_codesearch_I/?callback=?&q=${ qParams } &p=${ page } &per_page=42${ langParams } ` ;
74+ const done = data => {
75+ const cdata = {
76+ searchValue : val ,
77+ page : page ,
78+ variableList : [ ...this . _data . variableList , this . _parseVariableList ( data . results , q ) ] ,
79+ searchLang : lang ,
80+ suggestion : suggestion ,
81+ isZH : isZH || this . isZH
82+ } ;
83+ this . update ( cdata ) ;
84+ this . _variableListStore . save ( cacheId , cdata ) ;
85+ } ;
7486 val && JSONP ( url , { callbackName : 'searchcodeRequestVariableCallback' } )
75- . then ( data => {
76- const cdata = {
77- searchValue : val ,
78- page : page ,
79- variableList : [ ...this . _data . variableList , this . _parseVariableList ( data . results , q ) ] ,
80- searchLang : lang ,
81- suggestion : suggestion ,
82- isZH : isZH || this . isZH
83- } ;
84- this . update ( cdata ) ;
85- this . _variableListStore . save ( cacheId , cdata ) ;
86- } ) . catch ( err => {
87- this . update ( {
88- searchValue : val ,
89- page : page ,
90- variableList : [ ...this . variableList , [ ] ] ,
91- searchLang : lang ,
92- suggestion : suggestion ,
93- isZH : isZH || this . isZH
94- } ) ;
87+ . then ( done ) . catch ( ( ) => {
88+ // fallback to fetch
89+ fetch ( `//searchcode.com/api/codesearch_I/?q=${ qParams } &p=${ page } &per_page=42${ langParams } ` )
90+ . then ( res => res . json ( ) )
91+ . then ( done )
92+ . catch ( ( ) => {
93+ this . update ( {
94+ searchValue : val ,
95+ page : page ,
96+ variableList : [ ...this . variableList , [ ] ] ,
97+ searchLang : lang ,
98+ suggestion : suggestion ,
99+ isZH : isZH || this . isZH
100+ } ) ;
101+ } ) ;
95102 } ) ;
96103 }
97104
0 commit comments