0

根据 ember-data 中实现的测试,当我们从 hasMany 关系中请求子记录时,store 对子资源 url 进行 get GET 并发送所需子资源的 id。

test("finding many people by a list of IDs", function() {
  store.load(Group, { id: 1, people: [ 1, 2, 3 ] });

  var group = store.find(Group, 1);

  equal(ajaxUrl, undefined, "no Ajax calls have been made yet");

  var people = get(group, 'people');

  equal(get(people, 'length'), 3, "there are three people in the association already");

  people.forEach(function(person) {
    equal(get(person, 'isLoaded'), false, "the person is being loaded");
  });

  expectUrl("/people");
  expectType("GET");
  expectData({ ids: [ 1, 2, 3 ] });

我怎样才能发送父记录(组)的 id ?我的服务器需要这个 id 来检索嵌入的记录。它需要类似的东西:

expectData({groud_id: the_group_id, ids: [1,2,3] })
4

1 回答 1

1

您无法传递其他参数,并且从今天开始,资源预计将位于“根目录”。这意味着,在你的config/routes.rb

resources :organization
resources :groups
resources :people

乍一看,您可能会感到害怕,“天啊,我正在失去数据隔离......”,但实际上这种隔离最终通常是由关系连接提供的,从拥有嵌套内容的祖先开始。无论如何,这些连接可以由 ORM 以(合理的)价格执行,以声明祖先中的任何叶子资源。

假设您使用的是 RoR,您可以在模型中添加关系快捷方式以确保像这样的嵌套资源隔离(请注意 哪些是重要的东西)has_many ... through ...

class Organization < ActiveRecord::Base
  has_many :groups
  has_many :people, through: groups
end

class Group < ActiveRecord::Base
  has_many :people
end

然后在您的控制器中,直接使用快捷方式,在您的根持有人模型中展平(此处 Organization

class GroupsController < ApplicationController
  def index
    render json: current_user.organization.groups.all, status: :ok
  end
end

class PeopleController < ApplicationController
  def index
    render json: current_user.organization.people.all, status: :ok
  end
end

(这里,为了更清楚起见,返回整个现有实例集。应根据请求的 id 过滤结果...)

于 2012-07-18T14:00:22.457 回答