简体   繁体   中英

Load assets in Rails 3.2 with Modernizr.load

I want to be able to load in certain JS files using Modernizr.load --

 Modernizr.load({
  test : Modernizr.touch,
  yep  : '/assets/mobile.js',
  nope : '/assets/desktop.js',
});

but when this get compiled for production, those paths do not exist anymore. How can I sort out loading in this way?

You will need to add those assets to config.assets.precompile in your environment config file (found in config/environments/production.rb). Here's an example:

config.assets.precompile += %w(mobile.js desktop.js)

These assets can then be referenced by Modernizr:

Modernizr.load({
  test : Modernizr.touch,
  yep  : '/assets/mobile.js',
  nope : '/assets/desktop.js',
});

EDIT: You should also make sure these assets are NOT included in your application.js manifest. Including them in application.js would defeat the purpose of conditionally loading them for specific feature-less browsers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM