简体   繁体   中英

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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