Ember Data nested resource URL

前端 未结 1 1656
无人及你
无人及你 2020-12-30 16:46

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
         


        
相关标签:
1条回答
  • 2020-12-30 16:55

    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 to false in ApplicationSerializer.


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

    0 讨论(0)
提交回复
热议问题