JS Asset Paths
Inspired by js-routes, JS Asset Paths makes the helper methods found in ActionView::Helpers::AssetUrlHelper available to the client.
Installation
Add this line to your application's Gemfile:
gem 'js-asset_paths'
Then require the js-asset_paths file in your application.js
.
/*
= require js-asset_paths
*/
Ensure that your config/environments/production.rb
has the following:
config.assets.compile = true
(This avoids a NoMethodError: undefined method each_file for nil:NilClass
when
precompiling your assets upon deployment.)
Usage
A global object called PathHelper
will be created with the following methods:
- assetPath(source[, options])
- audioPath(source)
- fontPath(source)
- imagePath(source)
- javascriptPath(source)
- stylesheetPath(source)
- videoPath(source)
These functions operate in the same manner as their server-side counterparts.
Explanation
JS Asset Paths inspects the configuration options for the current environment and determines whether or not a digest will be appended to the pathname of asset files. An object correlating the truncated asset path with the actual path to the asset on the server is stored in the generated javascript and referenced during each call.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request