Making Loopback API Ember.js compatible
Asked Answered
S

1

6

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.

Shope answered 19/9, 2014 at 21:34 Comment(8)
What does "explorer" mean?Strontium
Ember doesn't require your JSON to be anything other than JSON. I think you are referring to the default REST Adapter of Ember Data. Ember.js does not require you to use Ember Data. And even if you do, you can write a custom adapter to handle any kind of JSON sent.Placeeda
Maybe a silly comment as I'm only just getting to grips with Loopback, but in the code above you are checking if ctx.result is non-null then setting ctx.res. Should you not be using ctx.result.body = ...?Acquirement
@Acquirement I'm not sure to be honest, will hopefully be looking at this again soon.Shope
@Placeeda thanks, yes we've realised that we should be looking to transform Ember's side rather than the API side.Shope
@Placeeda what about the case where you want to send metadata along with your response? e.g. { posts: [{ ... }, ...], meta: { ... }}Shope
@Shope I personally don't know much about writing Ember Data adapters since I don't use Ember Data. I just use ic.ajax (similar to JQuery but real promises) to return the data from my API and populate it as my model. Then I just refer directly to the model itself in the controller (or a clone of it). For more info, search Google for "using Ember without Ember Data".Placeeda
@Adamski: Did you make any progress with connecting Ember to Loopback? I'm considering doing this, wondering what others' experiences were with it...Abruption
H
1

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

Hebbe answered 8/9, 2015 at 21:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.