ember.js - Rails-style polymorphic model in ember-data -


if have following models in rails, how represent in 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 

references i've found the relevant ember-data pull request, the ember-data tests polymorphic relationships, , this related question it's difficult work out canonical example them.

i use 2 different ways of working rails-style "polymorphic" models. have updated code below show both uses.

attachment model: "polymorphic" on rails side "embedded" on ember side. reason need save/update attachments along associated model.

comment model: polymorphic on both rails side , ember side.

i have included code post model can have multiple attachments , multiple comments.


rails code:

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 |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 code:

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 set widget wraps 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   # should instantiated it's model set 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() 

unfortunately rails code has become little polluted ember-data specific oddities tries send attributes defined on models. (note: there open proposal read-only attributes solve params pollution issue)

if knows better way approach of above please let me know!

nb: i'm little concerned having models extend app.commentable prevent me having multiple polymorphic attachments on model, may need different way of handling that.


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -