1

有问题的宝石是:https ://github.com/rails/acts_as_tree

我使用acts_as_tree 作为在我的应用程序上嵌套和输出评论的一种方法。_comment.html.erb 中的以下代码可以正常工作。

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

所以我知道 comment.children 部分正在工作。但是,我正在尝试将 Ember 用作 Rails 应用程序的前端,但我不知道如何将其渗透到 ember 应用程序中。

我的评论模型(在rails中)如下:

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

我的评论序列化程序如下:

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

我的 Ember 评论模型如下:

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})
});

我认为我的问题有点宽泛,但我一直在研究自我参照关系,但由于我不太确定acts_as_tree 本身是如何工作的,因此我在实施任何这些解决方案方面都没有成功。我可能一次尝试做太多事情;这是我非常感谢您的帮助:

如何正确序列化acts_as_tree 功能以便Ember 可以读取它?

4

0 回答 0