Skip to content

Instantly share code, notes, and snippets.

@markoa
Created September 30, 2011 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markoa/1253148 to your computer and use it in GitHub Desktop.
Save markoa/1253148 to your computer and use it in GitHub Desktop.
module ApplicationHelper
# In your layout: <body <%= render_page_data %>>
#
def render_page_data
@page_data_attributes.html_safe if @page_data_attributes.present?
end
# Appends a hash of data attributes to the body element. For example:
#
# <% page_data :prefix => "bids", :bid_status => "finished" %>
#
# will render within the body tag as
#
# <body data-prefix="bids" data-bid-status="finished">
#
# Use this method to pass data to CoffeScript functions.
#
def page_data(options = {})
@page_data_attributes = ""
options.each do |key, val|
@page_data_attributes << "data-#{key.to_s.gsub("_", "-")}=\"#{val}\" "
end
@page_data_attributes
end
end
updateBidStatus = (bidPath, pollingPath) ->
$("#bidStatus").smartupdater url: pollingPath, (data) ->
# do more stuff
$ ->
return if pageAttr("prefix") != "bids"
unless pageAttr("bid-status") == "finished"
updateBuildStatus(pageAttr("bid-path"), pageAttr("bid-polling-path"))
<% unless @bid.finished? -%>
<%= content_for :head do %>
<%= coffee_script_tag do %>
$ ->
updateBidStatus("<%= bid_path(@bid) %>", "<%= polling_bid_path(@bid) %>")
<% end %>
<% end %>
<% end -%>
<%= content_for :head do %>
<script>
$(document).ready(function() {
<% if @bid.finished? -%>
Bids.updateStatus("<%= bid_path(@bid) %>", "<%= polling_pid_path(@bid) %>");
<% end -%>
});
</script>
<% end %>
<h1>Welcome...</h1>
# pageAttr("prefix") returns the value of "data-prefix" attribute on the body
# element
window.pageAttr = (attributeName) ->
$("body").attr("data-#{attributeName}")
window.updateBidStatus = (bidPath, pollingPath) ->
$("#bidStatus").smartupdater url: pollingPath, (data) ->
# do stuff
var Bids = {
updateStatus: function(bidPath, pollingPath) {
$("#bidStatus").smartupdater({ url: pollingPath }, function (data) {
// do stuff
});
}
};
<% page_data :prefix => "bids",
:bid_status => @bid.status,
:bid_path => bid_path(@bid),
:bid_polling_path => polling_bid_path(@bid) %>
<h1>Welcome...</h1>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment