Ember 数据嵌套资源 URL [英] Ember Data nested resource URL

查看:19
本文介绍了Ember 数据嵌套资源 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个具有以下布局的 Rails 应用程序(从我的实际项目中简化了一点):

Let's say I have a Rails app with the following layout (simplified this a bit from my actual project):

User
    has many Notes

Category
    has many Notes

Note
    belongs to User
    belongs to Category

笔记可以在以下位置获得:

Notes can either be obtained at:

/users/:user_id/notes.json
/categories/:category_id/notes.json

但不是:

/notes.json

整个系统中的注释太多,无法在一个请求中发送 - 唯一可行的方法是仅发送必要的注释(即属于用户试图查看的用户或类别的注释).

There are too many Notes across the whole system to send down in one request - the only viable way is to send only the necessary Notes (i.e. Notes that belong to either the User or Category the user is trying to view).

我使用 Ember Data 实现这一点的最佳方式是什么?

What would my best way of implementing this with Ember Data be?

推荐答案

我想说的很简单:

Ember 模型

App.User = DS.Model.extend({
  name: DS.attr('string'),
  notes: DS.hasMany('App.Note')
});

App.Category = DS.Model.extend({
  name: DS.attr('string'),
  notes: DS.hasMany('App.Note')
});

App.Note = DS.Model.extend({
  text: DS.attr('string'),
  user: DS.belongsTo('App.User'),
  category: DS.belongsTo('App.Category'),
});

Rails 控制器

class UsersController < ApplicationController
  def index
    render json: current_user.users.all, status: :ok
  end

  def show
    render json: current_user.users.find(params[:id]), status: :ok
  end
end

class CategoriesController < ApplicationController
  def index
    render json: current_user.categories.all, status: :ok
  end

  def show
    render json: current_user.categories.find(params[:id]), status: :ok
  end
end

class NotesController < ApplicationController
  def index
    render json: current_user.categories.notes.all, status: :ok
    # or
    #render json: current_user.users.notes.all, status: :ok
  end

  def show
    render json: current_user.categories.notes.find(params[:id]), status: :ok
    # or
    #render json: current_user.users.notes.find(params[:id]), status: :ok
  end
end

注意:这些控制器是一个简化版本(索引可能会根据请求的 id 进行过滤,...). 你可以看看 如何使用 ember 数据获取 parentRecord id 以供进一步讨论.

Be careful: these controllers are a simplified version (index may filter according to requested ids, ...). You can have a look at How to get parentRecord id with ember data for further discussion.

主动模型序列化器

class ApplicationSerializer < ActiveModel::Serializer
  embed :ids, include: true
end

class UserSerializer < ApplicationSerializer
  attributes :id, :name
  has_many :notes
end

class CategorySerializer < ApplicationSerializer
  attributes :id, :name
  has_many :notes
end

class NoteSerializer < ApplicationSerializer
  attributes :id, :text, :user_id, :category_id
end

我们在此处包含旁加载数据,但您可以避免它,将 include 参数设置为 ApplicationSerializer 中的 false.

We include sideload data here, but you could avoid it, setting the include parameter to false in ApplicationSerializer.

用户、类别和笔记将被收到 &由 ember-data 缓存,并根据需要请求丢失的项目.

The users, categories & notes will be received & cached by ember-data as they come, and missing items will be requested as needed.

这篇关于Ember 数据嵌套资源 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆