Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

automatically load a javascript function after loading page

I'm new to Rails and didn't really understand the asset pipeline by now…

I want to let a

views/product/product.js 

automatically fire after the

views/product/index.html.erb 

was rendered for DRY reasons.

Is there a place in the asset pipeline that calls a model.js file after loading any or a partial model.erb file?

I know how to do it manually, and dropped a

app/assets/javascripts/product.js

but then i have to call a doSomethingAfterPageload() Method in new, show. delete etc. Even better, if this works for partials as well.

like image 832
user3320429 Avatar asked Feb 20 '14 16:02

user3320429


1 Answers

The asset pipeline under a vanilla configuration is just (simplified explanation) going to take all of js files referenced in the manifest (application.js) and create single, minified, obfuscated file for production. In development, if you include the manifest in your page (should be the layout), you'll end up with js script include tags for each asset that the manifest contains.

A common pattern is to put at yield :javascript block in your layout and then in the individual view, call the javascript function inside of a content_for :javascript do block.

Best way to add page specific javascript in a Rails 3 app?

A fancier approach would be to conditionally execute js based on the controller and action. Here's how that works: http://viget.com/inspire/extending-paul-irishs-comprehensive-dom-ready-execution

like image 100
steakchaser Avatar answered Sep 28 '22 07:09

steakchaser