0

I'm using ember-select-2 as a typeahead in ember application.the problem is i can fetch the data from the server but the data isn't showing in dropdown.any help would be appreciated.Thanks in advance.

 {{select-2
    placeholder="Choose from our many pizzas"
    value=chosenTypeaheadPizza
    typeaheadSearchingText="Searching pizzas"
    typeaheadNoMatchesText="No pizzas found for '%@'"
    typeaheadErrorText="Loading failed: %@"
    query="queryPizzas"
 }}

and action handler is

queryPizzas(query) {    
        var self = this;
        var store = self.get('store');
        let adapter = store.adapterFor("pizzas"); 
        let serachQuery = query.term;      
           adapter.searchPizza(serachQuery).then(function(response) {

                console.log(response.pizzas);

           }); 
    },

Response

    {
    "pizzas": [{
        "id": 1,
        "name": "pizza 1"
    }, {
        "id": 2,
        "name": "pizza 2"
    }]
   }
Vivekraj K R
  • 2,418
  • 2
  • 19
  • 38

1 Answers1

1

If you check the examples of ember-select-2; you can see that it is using deferred parameter passed to action handler to display the data. It says there "Make sure to not call query.callback directly but always use the provided deferred!". This means, you need to call deferred that is to be provided as the second argument to the action handler. I am not expert at ember-select-2 but what you should do is probably

queryPizzas(query, deferred) {    
    var self = this;
    var store = self.get('store');
    let adapter = store.adapterFor("pizzas"); 
    let searchQuery = query.term;      
    adapter.searchPizza(searchQuery).then(function(data) {
      //try to pass the array as the data
      deferred.resolve({data: data.pizzas, more: false});
    }, deferred.reject);
}

Provided solution above works for the data format you have provided. Please check the corresponding twiddle. In this example; I have used a mock promise to simulate server remote call and returned the example data you have provided as the content to be displayed in select. You have to use optionLabelPath in order to display name of pizzas in the select as can be seen in application.hbs.

feanor07
  • 3,328
  • 15
  • 27
  • Now it shows in console TypeError: Cannot read property 'length' of undefined.i have added the response data. – Vivekraj K R Mar 16 '17 at 13:47
  • @vivek as I said I never used the component myself; but only giving it a try. I have update the answer in regards to your response data. Can you give it a try? – feanor07 Mar 16 '17 at 13:56
  • Still it shows TypeError: Cannot read property 'length' of undefined – Vivekraj K R Mar 17 '17 at 04:14
  • There are two properties for each object in the response array and i need to show the name of the pizza so how the addon will detect its name property? – Vivekraj K R Mar 17 '17 at 04:44
  • @vivek sorry I did not have time for the weekend to check this out. Please see the twiddle I have provided in the answer. My provided answer should work as is. Can you make sure data arrives from the server as you expected? – feanor07 Mar 20 '17 at 11:57
  • Thank you. You just nailed it. – Vivekraj K R Mar 21 '17 at 04:38