Created
September 30, 2011 08:51
-
-
Save markoa/1253148 to your computer and use it in GitHub Desktop.
Evolving use of CoffeeScript in Rails - supplement to http://renderedtext.com/blog/2011/09/30/a-convention-for-using-coffeescript-with-rails/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% unless @bid.finished? -%> | |
<%= content_for :head do %> | |
<%= coffee_script_tag do %> | |
$ -> | |
updateBidStatus("<%= bid_path(@bid) %>", "<%= polling_bid_path(@bid) %>") | |
<% end %> | |
<% end %> | |
<% end -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pageAttr("prefix") returns the value of "data-prefix" attribute on the body | |
# element | |
window.pageAttr = (attributeName) -> | |
$("body").attr("data-#{attributeName}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.updateBidStatus = (bidPath, pollingPath) -> | |
$("#bidStatus").smartupdater url: pollingPath, (data) -> | |
# do stuff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Bids = { | |
updateStatus: function(bidPath, pollingPath) { | |
$("#bidStatus").smartupdater({ url: pollingPath }, function (data) { | |
// do stuff | |
}); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% 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