如何在Ruby on rails上从视图访问pubnub的已发布属性

时间:2014-06-20 10:02:34

标签: ruby-on-rails handlebars.js pubnub

在我的rails项目中,我使用pubnub实时更新一些内容,并使用把手进行实时更新模板。以下是发布更新的代码

subscriber_channels.each do |subscriber_channel|
            $pubnub_server_subscription.publish(
                :channel => subscriber_channel,
                :message => {event: 'LIVE_FEED',
                             attributes: {
                                 live_feed_id: self.id,
                                 feedable_type: self.feedable_type,
                                 feedable_id: self.feedable_id,
                                 profile_picture_url: "#{self.action_user.profile_photo_thumb}",
                                 action_user_name: "#{self.action_user.full_name}",
                                 live_feed_content: "#{self.content}",
                                 feed_time: "#{self.created_at.strftime("%H:%M")}",
                                 reference_post_id: reference_post_id,
                                 reference_post_type: self.reference_post_type
                             }
                },
                callback: lambda { |info| puts info }
            )
          end

和模板我的代码在

下面
PUBNUB_CLIENT.events.bind("LIVE_FEED", function (message) {
var livefeedTemplate = Handlebars.compile($('#top-live-feed-template').html());
              var outputHtml = livefeedTemplate(message.attributes);
              $('#live-feed-table').prepend(outputHtml);
}

到目前为止,上面的代码工作正常。但是在调用模板之前我需要做一些检查。我想要这样的东西

    PUBNUB_CLIENT.events.bind("LIVE_FEED", function (message) {
if feedable_type == "image" #this feedable_type from published attributes above
        var livefeedTemplate = Handlebars.compile($('#top-live-feed-template').html());
                      var outputHtml = livefeedTemplate(message.attributes);
                      $('#live-feed-table').prepend(outputHtml);
        }
    }

feedable_type这里是上面发布的属性。那我该怎么做呢?或者我该怎么办?

console.log( "update a <div> with received event: feedable_type");

1 个答案:

答案 0 :(得分:0)

试试这个:

PUBNUB_CLIENT.events.bind("LIVE_FEED", function (message) {
    if(message.attributes.feedable_type == "image"){
        var livefeedTemplate = Handlebars.compile($('#top-live-feed-template').html());
        var outputHtml = livefeedTemplate(message.attributes);
        $('#live-feed-table').prepend(outputHtml);
    }
}

如果这不起作用,请提供更多详细信息。