Skip to content

Instantly share code, notes, and snippets.

@jamesarosen
Created November 13, 2009 21:04
Show Gist options
  • Save jamesarosen/234156 to your computer and use it in GitHub Desktop.
Save jamesarosen/234156 to your computer and use it in GitHub Desktop.
Push Javascript functionality to static files called with dynamic parameters
// put the view editing Javascript logic in a static .js file:
MyApp = {
onCreateReview: function(flash, newReviewsCount, reviewPartial) {
$("#new_review").before('<div id="flash_notice">' + flash + '</div>');
$("#reviews_count").html(newReviewsCount);
$("#reviews").append(reviewPartial);
$("#new_review")[0].reset();
}
};
// views/reviews/create.js.erb
// from http://railscasts.com/episodes/136-jquery
MyApp.onCreateReview(
"<%= escape_javascriptflash.delete(:notice)) -%>",
"<%= pluralize(@review.product.reviews.count, 'Review') %>",
"<%= escape_javascript(render(:partial => @review)) -%>");
// views/reviews/create.js.erb
// from http://railscasts.com/episodes/136-jquery
/* This puts too much logic in a dynamic view. That makes it
hard to unit test. Instead, change this to the
much_better.js.erb version and add the above code
to application.js
*/
$("#new_review").before('<div id="flash_notice"><%= escape_javascript(flash.delete(:notice)) %></div>');
$("#reviews_count").html("<%= pluralize(@review.product.reviews.count, 'Review') %>");
$("#reviews").append("<%= escape_javascript(render(:partial => @review)) %>");
$("#new_review")[0].reset();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment