Running the Loopback API Ember.js API

I am testing Loopback for an API that will talk to Ember.

Ember requires JSON to be contained in "keys", for example. for account:

{ account: { domain: 'domain.com', subdomain: 'test', title: 'test.domain.com', id: 1 } } 

I found some tips in the google group on how to change the answer so that Ember gets it using hook.

eg. on my /account.js models:

 module.exports = function(Account) { Account.afterRemote('**', function (ctx, account, next) { if(ctx.result) { if(Array.isArray(ctx.result)) { ctx.res.body = { 'accounts': account }; } else { ctx.res.body = { 'account': account }; } } console.log(ctx.res.body); next(); }); }; 

I see that the answer is the same as in the console. However, the JSON output on localhost: 3000 / api / accounts does not show the modified JSON object.

What is the correct way to modify JSON responses / requests in Loopback?

Ideal in the general case, so it can be applied to all models.

+6
source share
1 answer

You can make Ember data compatible with Strongloop loopback api using DS.RESTAdapter with DS.JSONSerializer as follows:

 // app/adapters/application.js import DS from 'ember-data'; export default DS.RESTAdapter.extend({ host: 'http://loopback-api-host', namespace: 'api', defaultSerializer: 'JSONSerializer' }); 

http://emberjs.com/api/data/classes/DS.JSONSerializer.html

β€œIn Ember Data, the logic for communicating with the data warehouse is stored in the adapter. The Ember Data Adapter has some built-in assumptions about what the REST API should look like. If your backend conventions are different from these assumptions, Ember Data makes it easy to change its functionality. by replacing or expanding the default adapter. "

http://guides.emberjs.com/v2.0.0/models/customizing-adapters/

Similar question: Strongloop with Emberjs

+1
source

Source: https://habr.com/ru/post/975560/


All Articles