ember.js - ember-data 中 Rails 风格的多态模型

标签 ember.js ember-data

如果我在 Rails 中有以下模型,我将如何在 Ember/Ember Data 中表示它?

class Attachment < ActiveRecord::Base
  belongs_to :user
  belongs_to :attachable, polymorphic: true
end

class Profile < ActiveRecord::Base
  belongs_to :user
  has_one :photo, class_name: 'Attachment', as: :attachable
end

class Post < ActiveRecord::Base
  belongs_to :user
  has_many :attachments, as: :attachable
end

我找到的引用文献是 the relevant ember-data pull request , the ember-data tests for polymorphic relationships , 和 this related question但是很难从它们中找出一个典型的例子。

最佳答案

我现在使用两种不同的方式来处理 Rails 风格的“多态”模型。我已经更新了下面的代码以显示这两种用途。

Attachment型号 :这在 Rails 端是“多态的”,但在 Ember 端总是“嵌入”的。这样做的原因是我目前只需要保存/更新附件及其关联模型。

Comment型号 :这在 Rails 端和 Ember 端都是多态的。

我还包含了 Post 的代码模型,因为它可以有多个附件和多个评论。

导轨代码:

class Attachment < ActiveRecord::Base
  belongs_to :user
  belongs_to :attachable, polymorphic: true
end

class Comment < ActiveRecord::Base
  belongs_to :user
  belongs_to :commentable, polymorphic: true
end

class Post < ActiveRecord::Base
  belongs_to :user
  has_many :attachments, as: :attachable
  has_many :comments, as: :commentable
end

class ApplicationSerializer < ActiveModel::Serializer
  embed :ids, include: true
end

class AttachmentSerializer < ApplicationSerializer
  attributes :id, :url, :errors
  has_many :comments
end

class CommentSerializer < ApplicationSerializer
  attributes :id, :body, :created_at, :commentable_id, :commentable_type
  has_one :user
end

class PostSerializer < ApplicationSerializer
  attributes :id, :title, :body, :posted_at, :errors
  has_one :user
  has_many :attachments, embed: :objects, include: true
  has_many :comments
end

class Api::V1::PostsController < Api::V1::BaseController
  before_filter :auth_only!, only: :create

  def create
    # clean / capture ember-data supplied arguments
    params[:post].delete(:user_id)
    attachments_params = params[:post].delete(:attachments)

    @post = current_user.posts.new(params[:post])
    process_attachments(attachments_params)

    if @post.save
      render json: @post, status: 201
    else
      warden.custom_failure!
      render json: @post, status: 422
    end
  end

  protected

    def process_attachments(attachments_params)
      return unless attachments_params.present?

      attachments_params.each do |attachment_params|
        # ignore ember-data's additional keys
        attachment_params.delete(:created_at)
        attachment_params.delete(:user_id)

        attachment = @post.attachments.new(attachment_params)
        attachment.user = current_user
      end
    end
end

Ember 代码:
DS.RESTAdapter.configure 'App.Post',
  alias: 'Post'

DS.RESTAdapter.map 'App.Post',
  attachments: { embedded: 'always' }

App.Store = DS.Store.extend
  adapter: DS.RESTAdapter.create
    namespace: 'api/v1'

App.Comment = App.Model.extend
  user: DS.belongsTo('App.User')
  commentable: DS.belongsTo('App.Commentable', { polymorphic: true })
  body:       DS.attr('string')
  createdAt:  DS.attr('date')

App.Commentable = App.Model.extend
  comments: DS.hasMany('App.Comment')

App.Attachment = App.Commentable.extend
  user: DS.belongsTo('App.User')
  url: DS.attr('string')

App.Post = App.Commentable.extend
  user: DS.belongsTo('App.User')
  attachments: DS.hasMany('App.Attachment')

  title: DS.attr('string')
  body: DS.attr('string')
  postedAt: DS.attr('date')

App.PostFormOverlayController = App.OverlayController.extend
  # 'files' attribute is set by a widget that wraps the filepicker.io JS
  updateAttachments: (->
    attachments = @get('attachments')
    attachments.clear()
    @get('files').forEach (file) =>
      attachment = App.Attachment.createRecord({fpFile: file})
      attachments.addObject(attachment)
  ).observes('files')

App.CommentNewController = App.ObjectController.extend
  # this should be instantiated with it's model set to the commentable
  # item. eg. `{{render 'comment/new' content}}`

  save: ->
    transaction = @get('store').transaction()
    comment = transaction.createRecord App.Comment,
      body: @get('body')
      commentable: @get('model')

    comment.one 'didCreate', @, ->
      @set('body', null)

    transaction.commit()

不幸的是,我的 Rails 代码在尝试发回模型上定义的所有属性时,已经被 ember-data 特定的奇怪东西污染了。 ( 注意 :有一个 open proposal 用于解决参数污染问题的只读属性)

如果有人知道处理上述任何一项的更好方法,请告诉我!

注意:我有点担心模型从 App.Commentable 扩展将阻止我在模型上拥有多个多态附件,我可能需要寻找不同的处理方式。

关于ember.js - ember-data 中 Rails 风格的多态模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16561173/

相关文章:

javascript - 完整的 emberjs 数据库驱动网站

javascript - 使用 ember_data 而不是 ember_model.js

javascript - Ember 操作中的事件委托(delegate)和全局 `event` 对象

javascript - 在 ember.js 中复制嵌套模型(成功保存后)

ember.js - 如何使用 Ember.JS 中的匹配来查找模型中的对象?

javascript - Ember,异步渲染嵌套模型集合

javascript - ember-power-select 中的自定义选项和第一个选项

ruby-on-rails - 使用 Ember.js & Ember-data & Rails 创建记录并处理记录列表

版本升级后Ember.js数据迁移

ember.js - EmberJS 2.13单元测试: wrapping synchronous code in a run loop?