[英]How to access published attributes of pubnub from view in Ruby on rails
在我的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");
嘗試這個:
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);
}
}
如果那行不通,請提供更多細節。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.