I'm trying to create a search coming from a JSON API. I have seen a few tutorials teaching how to do this and I got it working with their 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 however when I try to do the same with a promise I get 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 is the 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 is an example of the 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 get this error:
Uncaught TypeError: companies.filter is not a function
I tried figuring out a way to convert a promise into an array so I can run the filter function but came up with nothing. What is the correct way to achieve what I am trying to do?
Aucun commentaire:
Enregistrer un commentaire