本文介绍了如何使用Ember Data 1.0 beta打非REST端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了在某些资源上有一个 / search 终结点外,我的API大多数都比较安静。我正在使用 DS.ActiveModelAdapter DS.ActiveModelSerializer ,一切都很棒。

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)

问题是在这种情况下,我不知道如何执行适配器中发生的所有标准化/驼峰化。因为在这种情况下,模板依赖于 @get(组织),所以某些带下划线的属性不会显示。

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?

推荐答案

pushPayload可以进行规范化/骆驼化,但需要从文档中知道您要推送的类型...但是版本为v1.0.0-beta.3

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)

数据json是组织的数组

And the data json an array of organizations

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

这篇关于如何使用Ember Data 1.0 beta打非REST端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 15:26