Using RequireJS with frontal
RequireJS is a JavaScript loader and module manager. It’s great for loading scripts concurrently and without blocking the page from rendering.
Frontal now supports a simple plugin architecture. Using the .register method it’s possible to mix functionality directly into frontal.
After including RequireJS in my page I can mix it’s functionality into frontal with just one line of code:
$frn.register('require', require);
Now I can use frontal rules with RequireJS:
$frn.require('/path', ['include.js']);
A frontal plugin’s first argument is the path for the rule to match. Any subsequent arguments are passed directly to the function referenced in the call to .register and executed when the path matches the current location. Plugins will also work with the queuing functionality of frontal.
So now I can write frontal rules which automatically load my dependencies through RequireJS, triggering my callback when they have finished loading.
$frn.require('/path', ['include.js'], function(){ // Do something with the loaded scripts when they're ready. });
Frontal rules combined with the loading power of RequireJS are a great way to manage script dependencies while reducing request overhead.
The latest release of frontal with plugin support is available on github.
Make a trackback.