javascript - Isolating JS files to be view specific in Rails -


in rails app, have number of js files available, , have placed them in app/assets/javascripts/globals, compiling them application.js via //= require_tree ./globals.

however, have js files view specific, , prefer if implemented views or controllers. they're compiled in config/initializers/assets.rb via rails.application.config.assets.precompile += %w( foo.js bar.js ), , accessed via <%= javascript_include_tag "foo/bar", "data-turbolinks-track" => true %> in respective views.

i've wrapped essential functions in foo.js & bar.js in conditionals such if $("#foo").length > 1 ..., prevents functionality if required divs aren't present.

as result, js files aren't "active" until after view visited. however, after being visited, js code "active" after visiting view. there control mechanism ensure js code being read correlating views?

http://brandonhilkert.com/blog/page-specific-javascript-in-rails/ http://brandonhilkert.com/blog/organizing-javascript-in-rails-application-with-turbolinks/

this how i've implemented view specific, though it's real pain.

essentially, you'll building script namespace contains scripts triggered or on specific controllers or actions.

rails provides way of doing controller specific. http://guides.rubyonrails.org/asset_pipeline.html#controller-specific-assets

i'd try instead it's pretty simple. i'm supposing, looking @ implementation action based well.

as you're "active" code issue.

1) ensure using on document ready (and/or page:load turbolinks depending on version).

2) ensure scripts work begin with.

3) ensure they're working both page refresh , turbolink visit.

depending on how , when breaks, mean different problems.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -