Ember数据嵌套资源URL [英] Ember Data nested resource URL
问题描述
用户
有许多Notes
类别
有许多Notes
注意
属于用户
属于类别
笔记可以通过以下方式获得:
/users/:user_id/notes.json
/categories/:category_id/notes.json
但不是:
/notes.json
/ pre>
整个系统中有太多的Notes可以在一个请求中发送 - 唯一可行的方法是只发送必要的Notes(即,属于用户或类别,用户正在尝试查看)
我的最佳方法是使用Ember Data实现?
解决方案我会说简单:
Ember模型
App.User = DS.Model.extend({
name:DS.attr('string'),
notes:DS .hasMany('App.Note')
});
App.Category = DS.Model.extend({
name:DS.attr('string'),
note: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
#或
#render json:current_user.users.notes.all,status: :ok
end
def show
render json:current_user.categories.notes.find(params [:id]),状态::ok
#或
#render json:current_user.users.notes.find(params [:id]),状态::ok
end
end
请注意:这些控制器是一个简化版本(索引可能会根据请求的ids来筛选...)您可以看看如何使用ember数据获取parentRecord id 进一步讨论
活动模型序列化器
class ApplicationSerializer< ActiveModel :: Serializer
embed:ids,include:true
end
class UserSerializer< ApplicationSerializer
属性:id,:name
has_many:notes
end
class CategorySerializer< ApplicationSerializer
属性:id,:name
has_many:notes
end
class NoteSerializer< ApplicationSerializer
属性:id,:text,:user_id,:category_id
end
我们在这里包含侧装数据,但是您可以避免这种情况,将
包含
参数设置为false
ApplicationSerializer
。
用户,类别&票据将收到&根据ember数据缓存,并根据需要请求丢失的项目。
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
but not:
/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).
What would my best way of implementing this with Ember Data be?
解决方案I would say simple:
Ember models
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 controllers
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
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.
Active Model Serializers
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
We include sideload data here, but you could avoid it, setting the
include
parameter tofalse
inApplicationSerializer
.
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屋!