Strongloop with Emberjs

依然范特西╮ 提交于 2019-12-23 09:27:02

问题


Ember Data's REST Adapter accepts the JSON from the server in this format:

Taken from the documentation: http://guides.emberjs.com/v1.10.0/models/the-rest-adapter/

{
  "post": {
    "id": 1,
    "title": "Node is not omakase",
    "comments": [1, 2, 3]
  },

  "comments": [{
    "id": 1,
    "body": "But is it _lightweight_ omakase?"
  },
  {
    "id": 2,
    "body": "I for one welcome our new omakase overlords"
  },
  {
    "id": 3,
    "body": "Put me on the fast track to a delicious dinner"
  }]
}

Is it possible to have this kind of JSON format back from strongloop?


回答1:


Remote methods are not the best solution because they are per model, and thus not DRY.

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: Making Loopback API Ember.js compatible




回答2:


By default the out-of-box restful api endpoints would return something that looks more like:

{
    "id": 1,
    "title": "Node is not omakase",
    "comments": [
      {
        "id": 1,
        "body": "But is it _lightweight_ omakase?"
      },
      {
        "id": 2,
       "body": "I for one welcome our new omakase overlords"
      },
      {
        "id": 3,
        "body": "Put me on the fast track to a delicious dinner"
      }
    ]
}

But you can use remote methods to do the same work and then massage the data into the way you want it to be returned. http://docs.strongloop.com/display/public/LB/Remote+methods



来源:https://stackoverflow.com/questions/29863278/strongloop-with-emberjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!