How to use ruby acts_as_tree gem with Ember.js?

104 Views Asked by At

The gem in question is : https://github.com/rails/acts_as_tree

I used acts_as_tree as an approach to nesting and outputting comments on my app. The following code, in _comment.html.erb, works fine.

<div id="comment_<%= comment.id %>">
  <%= comment.body %> => <%= comment.children.length %> replies
  <%= render :partial => 'comments/comment', :collection => comment.children %>
</div>

so I know that the comment.children portion is working. However, I am trying to use Ember as the front end to my rails app, and I have no clue how to percolate this up to the ember application.

My comment model (in rails) is as follows:

class Comment < ActiveRecord::Base
    extend ActsAsTree::TreeView

    acts_as_tree :order => 'created_at'
    belongs_to :post
    belongs_to :user

    def commented_by
        self.user.username
    end
end

My comment serializer is as follows:

class CommentSerializer < ActiveModel::Serializer
    embed :ids, include: true

  attributes :id, :parent_id, :body, :created_at, :updated_at, :post_id, :user_id, :commented_by

  belongs_to :post
end

My Ember Comment model is as follows:

App.Comment = DS.Model.extend({
    parentID: DS.attr('number'),
    body: DS.attr('string'),

    createdAt: DS.attr('date'),
    updatedAt: DS.attr('date'),

    postID: DS.attr('number'),
    userID: DS.attr('number'),

    commentedBy: DS.attr('string'),

    post: DS.belongsTo('post', {async: true}),
    // parent: DS.belongsTo('comment', {async: true}),
    // children: DS.hasMany('comment', {async: true})
});

My question is a bit broad, I think, but I have been looking at self-referential relationships, but since I am not quite sure how acts_as_tree itself is working, I have not had success in implementing any of these solutions. I'm probably trying to do too much at once; here is what I would greatly appreciate help with:

How can I correctly serialize the acts_as_tree functionality so that Ember can read it?

0

There are 0 best solutions below