admin管理员组文章数量:1400031
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.
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.
Share Improve this question edited Feb 20, 2014 at 16:46 Guilherme Franco 1,48312 silver badges19 bronze badges asked Feb 20, 2014 at 16:27 user3320429user3320429 631 silver badge3 bronze badges3 Answers
Reset to default 4The 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 mon 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./inspire/extending-paul-irishs-prehensive-dom-ready-execution
IMO, if this js method should be called in each view of the product model, you can defined a layout for product, and called the method inside the layout file.
Taken from here: Asset Pipeline - Rails guides
If you add app/assets/javascripts/<controller>.js
it will be available for that particular controller.
So if you add to app/assets/javascripts/product.js
:
$(document).ready ->
alert "page has loaded!"
It will be executed in every view of that controller, like index
or edit
.
Hope this helps :)
本文标签: ruby on railsautomatically load a javascript function after loading pageStack Overflow
版权声明:本文标题:ruby on rails - automatically load a javascript function after loading page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744232097a2596385.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论