Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember Data nested resource URL

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?

like image 899
user1539664 Avatar asked Jul 20 '12 04:07

user1539664


1 Answers

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.

like image 192
Mike Aski Avatar answered Oct 11 '22 13:10

Mike Aski