【问题标题】:Rails Sync - Update a counter in layoutRails Sync - 更新布局中的计数器
【发布时间】:2015-06-26 20:40:50
【问题描述】:

我正在尝试在我的网站标题中显示一个 cmets 计数器,其中有几个选项:

ul
    = sync_new partial: "show", resource: Commontator::Comment.new
/ also with
ul
    = sync partial: "show", resource: Commontator::Comment.new
/ also with
ul
    = sync_new partial: "show", collection: Commontator::Comment.all
/ also with
ul
    = sync partial: "show", collection: Commontator::Comment.all

在我看来:

li
    a = comment.class.all.size

我不清楚sync_newsync 之间的区别。

我的理解:

  1. 在传递集合时,同步会为集合中的每个项目呈现部分内容,显示计数器没有意义。
  2. 传递资源时,sync 会为传递的资源渲染部分内容,问题是当创建或销毁另一个评论时,传递的资源不会更新计数器。

也许我应该走一个完全不同的方向,比如直接使用 Faye。任何建议表示赞赏。

还发布了here

【问题讨论】:

    标签: ruby-on-rails websocket publish-subscribe faye


    【解决方案1】:

    我将创建一个计数器缓存,以便在发布新评论时缓存更改。我将使用缓存渲染模型:

    = sync partial: 'discussion', resource: discussion
    

    然后,当创建、编辑、删除评论时,部分应该更新。这将是部分:

    li
      ul
        - discussion.thread.comments.each do |comment|
          li = comment.body
    

    【讨论】:

      【解决方案2】:

      您可以使用 Sync 的 Javascript 回调,例如 beforeInsertafterInsertbeforeUpdateafterUpdatebeforeDestroyafterDestroy(more info)

      您可以使用它们来增加/减少在插入或删除评论时显示在特定元素上的计数器:

      class Sync.CommentShow extends Sync.View
      
        # OVERRIDE
        afterInsert: ->
          commentsCount += 1;
          $('#counter-div').text(commentsCount);
      
        # OVERRIDE
        afterRemove: ->
          commentsCount -= 1;
          $('#counter-div').text(commentsCount);
      

      我希望你和其他观众觉得这很有用。

      【讨论】:

        猜你喜欢
        • 2019-12-18
        • 2020-09-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-03
        • 1970-01-01
        相关资源
        最近更新 更多