Created
November 13, 2009 21:04
-
-
Save jamesarosen/234156 to your computer and use it in GitHub Desktop.
Push Javascript functionality to static files called with dynamic parameters
This file contains hidden or 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
// 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(); | |
} | |
}; |
This file contains hidden or 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
// 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)) -%>"); |
This file contains hidden or 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
// 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