My goal is to develop a search functionality using a JSON API. After following tutorials and successfully implementing it with provided 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')
});
Although this approach works effectively, I encountered difficulties when attempting the same operation with a promise:
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')
});
Below is the adapter class structure:
export default Ember.Object.extend({
findAll: function(){
return ajax('http://localhost:8000/api/v1/address-book/companies')
.then(function(response){
return response.data;
});
}
});
An illustration of the JSON API response format:
{
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"
}
]
}
]
}
The error message received states:
Uncaught TypeError: companies.filter is not a function
I have researched methods to convert a promise into an array for filtering but have not found a solution yet. Any guidance on how to accomplish my objective would be greatly appreciated.