javascript - Push data in Ember store in service layer and return promise -


i using ember 2.7.0 in 1 of ember service returning hardcoded(yet call rest endpoint) data instead want return promise hardcoded data. in controller if promise success push hardcoded data ember store otherwise return error object.i entirely new ember don't know how achieve this.

below service file

customer.js

import ember '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 above service class instead of returning hardcoded json should return rsvp promise along data.

below controller file

index.js

import ember '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 serializer file

customer.js

import applicationserializer './application';  export default applicationserializer.extend({  primarykey: 'customerid' }); 

3-things resolve in above code :

1) have return promise service along data.

2) how pushed data(hope pushed data correctly).

3) while running above code getting following exception,

error: ember data request /api/v1/customers returned 404 

it great if guide me resolve these issues.

1) first part of question simple. in service, can this:

import ember 'ember';  export default ember.service.extend({   getindividualcustomer(id) {     return new ember.rsvp.promise(function(resolve) {       resolve({ ...data...});   }) } 

2) in controller action, have pushed data store. therefore, once transition customers.view route, can retrieve customer in store via id. in customers.view route's model hook, this:

import ember 'ember';  export default ember.route.extend({   model(params) {     return this.store.find('customer', params.id)   } }) 

then in controller have access customer retrieved in route's model hook.

3) hard resolve since none of code laid out above (except commented out code) should making request server. 1 ajax request have (commented out) not making request /customers going /customers/:id. somewhere else in route, customers route, you're making request.


Comments