Ember services for the fetch data


#1

I am using ember ( v 3.0) service to fetch customer data . when i am calling same service from template , it not showing any data . following are the code

//service.js
export default Service.extend({
  store: Ember.inject.service(),
  cutomers : null,

  getProperty() {
     var list = this.get('store').findAll('customer');
     this.set('cutomer', list );
     return this.get('cutomers');
   }
});
//controller.js 
export default Controller.extend({
    customerList: Ember.inject.service('customers-list'),
});
{{! template.js }}
<select ui-jq="chosen" class="w-full">
   {{#each customersList.cutomers as |item|}}
      <option value={{item.id}}> {{item.name}} </option>
   {{/each}}
</select>

#2

The code is not well formatted, but if I got it right, customers property of your customerList service is null. getProperty method of your service sets a customer property to a PromiseProxy returned by ember-data’s store service. I think this should set customers property, isn’t it? So to put it together: there seems to be a typo and I don’t see getProperty called anywhere in the code snippets you provided. Make sure it’s called.

In general I don’t think this is good code. Is there any reason you are not using a Route’s model to load customerList? If fetching the data via service, I would use a computed property to make sure it’s fetched on first request.

import { inject as service } from '@ember/service';
import { computed } from '@ember/object';

export default Service.extend({
  store: service(),
  customers: computed(function() {
    return this.get('store').findAll('customer');
  })
});

This is doing the same as your service but does not require to execute getProperty() before customers property is available.


#3

ya i know its not gud code ( i m new on ember ) the reason behind not using route is customer is different model and it has heavy load . when i use through the route it take total 27 to 38 sec to load data.


#4

and thanx . when using computed property i got desire result . any suggestion please let me know . it will help me lot. Thanks


#5

I corrected the formatting in the original post for readability.

@Vishwajit_Kolekar: @jelhan wasn’t criticizing the content of your code, just the way it was formatted here on the forum. Here is a good reference on the Markdown you can use in posts. In this case, it was a “code block” that helped.

EDIT to add: well OK, maybe both were being crititiqued. :stuck_out_tongue:But it’s still helpful to separate format critique from content critique.