lundi 8 août 2016

Push data in Ember store in service layer and return promise

I am using ember 2.7.0 In one of my ember service i am returning an hardcoded(Yet to call rest endpoint) data instead i want to return the promise with the hardcoded data. In controller if the promise is success then push the hardcoded data to ember store otherwise return error object.I am entirely new to ember so i don't know how to achieve this.

Below is my service file

customer.js

import Ember from 'ember';

export default Ember.Service.extend({
  getIndividualCustomer(id) {
// return Ember.$.ajax({
//   url: `/api/v1/customers/${id}`,
//   type: 'GET'
// });
return {
  "customerId" : "38427357",
  "productName":[{
  "product":[{
    "name":"sample1",
    "status":"Active"
  },{
    "name":"sample2",
    "status":"Active"
  }]
  }]
};

}
});

In the above service class instead of returning the hardcoded json i should return RSVP promise along with this data.

Below is my controller file

index.js

import Ember from 'ember';

export default Ember.Controller.extend({

customer: Ember.inject.service(),

actions: {
searchCustomer: function() {
  this.store.push(this.store.normalize('customer', this.get('customer').getIndividualCustomer(this.get('inputID'))));
  this.transitionToRoute('customers.view', this.get('inputID'));
},
}
});

Below is my serializer file

customer.js

import ApplicationSerializer from './application';

export default ApplicationSerializer.extend({
 primaryKey: 'customerId'
});

3-Things to Resolve in the above code :

1) Have to return promise from the service along with data.

2) How to get the pushed data(Hope i pushed the data correctly).

3) While running above code i am getting the following exception,

Error: Ember Data Request GET /api/v1/customers returned a 404

It would be great if someone guide me to resolve these issues.




Aucun commentaire:

Enregistrer un commentaire