javascript - filter over a promise Emberjs -


i'm trying create search coming json api. have seen few tutorials teaching how , got working examples:

export default ember.arraycontroller.extend({     searchtext: null,      searchresults: function(){             var searchtext = this.get('searchtext');              if ( ! searchtext)             {                 return;             }             else             {                 var regex = new regexp(searchtext, 'i');                 return ['hey', 'dude'].filter(function(c){                     return c.match(regex);                 });             }         }.property('searchtext')  }); 

this works great when try same promise lost:

export default ember.arraycontroller.extend({     searchtext: null,      searchresults: function(){             var searchtext = this.get('searchtext');             var adapter = addressbookadapter.create();             var companies =  adapter.findall();              if ( ! searchtext)             {                 return;             }             else             {                 var regex = new regexp(searchtext, 'i');                 return companies.filter(function(c){                     return c.match(regex);                 });             }         }.property('searchtext')  }); 

here adapter class:

export default ember.object.extend({     findall: function(){         return ajax('http://localhost:8000/api/v1/address-book/companies')             .then(function(response){                 return response.data;             });     } }); 

here example of json api response structure:

{   data: [     {       id: 6,       name: "alexandrine skiles",       links: [         {           rel: "self",           uri: "/api/v1/address-book/alexandrine-skiles"         }       ]     },     {       id: 33,       name: "ally johns",       links: [         {           rel: "self",           uri: "/api/v1/address-book/ally-johns"         }       ]     }   ] } 

i error:

uncaught typeerror: companies.filter not function 

i tried figuring out way convert promise array can run filter function came nothing. correct way achieve trying do?

you can't "convert" promise array. promise promise object, may fulfilled @ point (if not fulfilled). can return results asynchronously.

so when have line:

var companies = adapter.findall(); 

the companies variable promise that, when fulfilled, return data. in other words, companies has .then() method should using.

so arraycontroller code become this:

export default ember.arraycontroller.extend({     searchtext: null,     searchresults: [],      searchresultupdater: function(){         var searchtext = this.get('searchtext');         var adapter = addressbookadapter.create();         var companies =  adapter.findall();          if ( ! searchtext)         {             return;         }         else         {             var regex = new regexp(searchtext, 'i');             companies.then(function(data) {                 var results = data.filter(function(c){                     return c.match(regex);                 });                 this.set('searchresults', results);             });         }     }.property('searchtext') }); 

this sets searchresults property when promise fulfilled. of course, there nicer ways of doing - ember provides lot of tools.

also may want if (!searchtext) bit before make ajax requests if you're not going use result @ ;)


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -