मैं सरल कहेंगे:
एंबर मॉडल
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'),
});
रेल नियंत्रक
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
सावधान: इन नियंत्रकों एक सरलीकृत संस्करण (सूचकांक अनुसार फिल्टर कर सकते हैं कर रहे हैं अनुरोध करने के लिए आईड्स, ...)। आगे चर्चा के लिए आप How to get parentRecord id with ember data पर एक नज़र डाल सकते हैं।
सक्रिय मॉडल 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
हम sideload डेटा यहाँ शामिल हैं, लेकिन आप इसे से बचने के सकता है, ApplicationSerializer
में false
करने के लिए include
पैरामीटर।
उपयोगकर्ताओं, श्रेणियों & नोटों & प्राप्त किया जाएगा एंबर-डेटा द्वारा कैश के रूप में वे आते हैं, और लापता आइटम के रूप में की जरूरत अनुरोध किया जाएगा।
क्या एम्बर डेटा स्वचालित रूप से उपयुक्त यूआरएल (/users/:user_id/notes.json या /categories/:category_id/notes.json) का उपयोग कर अनुरोध करेगा, बस एसोसिएशन के आधार पर? – user1539664
नहीं, यह '/ नोट्स 'का उपयोग करेगा, लेकिन आपका नियंत्रक शामिल होगा, (श्रेणियों | उपयोगकर्ताओं) से शुरू होने वाले रिश्ते को पार कर रहा है, इसलिए डेटासेट केवल उपयोगी उदाहरण तक ही सीमित होगा। –
तो श्रेणी और उपयोगकर्ता ऑब्जेक्ट दोनों के नोट्स तक पहुंच की इजाजत देने का कोई तरीका नहीं है? –