Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Loopback API Ember.js compatible

I'm trying out Loopback for an API that will talk to Ember.

Ember requires JSON to be contained in 'keys', e.g. for an account:

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

I've found some advice on the Google group about how to alter the response so that Ember will receive it, using afterRemote hooks.

E.g. in my models/account.js:

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 response is as it should be in the console .. however the JSON output at localhost:3000/api/accounts does not show the altered JSON object.

What is the correct way to alter the JSON response / requests in Loopback?

Ideally in a general way so it can be applied to all Models.

like image 880
Adamski Avatar asked Sep 19 '14 21:09

Adamski


People also ask

Does Ember use node js?

Ember CLI is built with JavaScript, and expects the Node. js runtime. It also requires dependencies fetched via npm.

Who use Ember js?

Who uses Ember. js? 506 companies reportedly use Ember. js in their tech stacks, including Twitch, LinkedIn, and Accenture.

Where is Ember js used?

Ember. js is a productive, battle-tested JavaScript framework for building modern web applications. It includes everything you need to build rich UIs that work on any device.

How does Ember js work?

Ember uses templates to organize the layout of HTML in an application. Ember templates use the syntax of Handlebars templates. Anything that is valid Handlebars syntax is valid Ember syntax. Here, {{name}} is a property provided by the template's context.


1 Answers

You can make Ember-data compatible with Strongloop's loopback api by using the DS.RESTAdapter with DS.JSONSerializer like this:

// 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 a backend data store lives in the Adapter. Ember Data's Adapter has some built-in assumptions of how a REST API should look. If your backend conventions differ from these assumptions Ember Data makes it easy to change its functionality by swapping out or extending the default Adapter."

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

Similar question: Strongloop with Emberjs

like image 196
Robert Carter Mills Avatar answered Oct 04 '22 20:10

Robert Carter Mills