How to hit non REST endpoints with Ember Data 1.0 beta

雨燕双飞 提交于 2019-12-11 18:55:45

问题


My API is mostly restful except I have a /search endpoint on some resources. I'm using the DS.ActiveModelAdapter and DS.ActiveModelSerializer and everything is great.

My current implementation for search is somewhat like this:

makeAPICall: ->
  @set('loading', true)

  states = @get('selectedStates')
  statesString = states.join(',')
  query = @get('searchParam')
  url = "/api/v1/organizations/search?#{statesString}&query=#{query}"

  $.get(url).then (data) =>
    @get('store').pushPayload(data)
    # TODO this needs to go through the adapter.
    orgs = data.organizations.map (org) =>
      @store.find('organization', org.id)
    @set('organizations', orgs)
    @set('loading', false)

The problem is that I don't know how to do all the normalization/camelization that happens in the adapter in this case. Because the template relies on the @get('organizations') in this case, some underscored attributes don't show up.

What is the correct way to implement this?


回答1:


The pushPayload is suposed to do the normalization/camelization but needs to know the type you are pushing, from the docs... but is in the v1.0.0-beta.3 version

var pushData = {
  posts: [
   {id: 1, post_title: "Great post", comment_ids: [2]}
  ],
  comments: [
    {id: 2, comment_body: "Insightful comment"}
  ]
}

store.pushPayload('post', pushData); 

In your case the call should be

@get('store').pushPayload('organization', data)

And the data json an array of organizations

organizations:[
    {id:1,...},
    {id:2,...},
    {id:3,...}
]


来源:https://stackoverflow.com/questions/19373446/how-to-hit-non-rest-endpoints-with-ember-data-1-0-beta

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