简体   繁体   English

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

[英]How to access published attributes of pubnub from view in Ruby on rails

In my rails project I use pubnub to live update few things and I use handlebars for live update template. 在我的rails项目中,我使用pubnub实时更新一些内容,并使用把手进行实时更新模板。 Below is the code to publish the update 以下是发布更新的代码

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

and for template my code is below 对于模板,我的代码如下

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

So far above code works just fine. 到目前为止,以上代码都可以正常工作。 But I need to do some checking before I call the template. 但是在调用模板之前,我需要做一些检查。 I want something like this 我想要这样的东西

    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 here is the published attributes from above. feedable_type是上面发布的属性。 So how can I do this? 那我该怎么做呢? Or how can I do something like this? 或者我该怎么做?

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

Try this: 尝试这个:

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

If that would not work, please, provide a bit more details. 如果那行不通,请提供更多细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM