2012-07-20 14 views
5

चलो कहते हैं कि मैं निम्नलिखित लेआउट के साथ एक रेल app (यह मेरा वास्तविक परियोजना से थोड़ा सरलीकृत) करते हैं:एंबर डाटा नेस्टेड संसाधन यूआरएल

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

User 
    has many Notes 

Category 
    has many Notes 

Note 
    belongs to User 
    belongs to Category 

नोट्स या तो पर प्राप्त किया जा सकता

लेकिन नहीं:

/notes.json 

वहाँ पूरी प्रणाली भर में बहुत सारे नोट्स एक अनुरोध में नीचे भेजने के लिए कर रहे हैं - केवल व्यवहार्य तरीका है केवल आवश्यक नोट्स भेजें (यानी। नोट्स जो उपयोगकर्ता या श्रेणी से संबंधित हैं, उपयोगकर्ता उपयोगकर्ता को देखने का प्रयास कर रहा है)।

एम्बर डेटा के साथ इसे लागू करने का मेरा सबसे अच्छा तरीका क्या होगा?

उत्तर

5

मैं सरल कहेंगे:

एंबर मॉडल

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 पैरामीटर।


उपयोगकर्ताओं, श्रेणियों & नोटों & प्राप्त किया जाएगा एंबर-डेटा द्वारा कैश के रूप में वे आते हैं, और लापता आइटम के रूप में की जरूरत अनुरोध किया जाएगा।

+0

क्या एम्बर डेटा स्वचालित रूप से उपयुक्त यूआरएल (/users/:user_id/notes.json या /categories/:category_id/notes.json) का उपयोग कर अनुरोध करेगा, बस एसोसिएशन के आधार पर? – user1539664

+0

नहीं, यह '/ नोट्स 'का उपयोग करेगा, लेकिन आपका नियंत्रक शामिल होगा, (श्रेणियों | उपयोगकर्ताओं) से शुरू होने वाले रिश्ते को पार कर रहा है, इसलिए डेटासेट केवल उपयोगी उदाहरण तक ही सीमित होगा। –

+0

तो श्रेणी और उपयोगकर्ता ऑब्जेक्ट दोनों के नोट्स तक पहुंच की इजाजत देने का कोई तरीका नहीं है? –

संबंधित मुद्दे