RequireJS is a library created to load js files asynchronous. It’s objective is to optimize developement and speed up the web page.
I do not cover any RequireJS documentation here. Let me say it ask you to write a piece of code like this in your HTML:
<script src="path/to/require.js" data-main="path/to/script"></script>
where the “src” attribute points to the RequireJS library and the “data-main” attribute points to the main script to be loaded. This is the entry point of your js app and here you can load other modules (AMD).
The problem: “data-main” needs a relative path to the document so you need to write a CakePHP piece of code like that:
// do you really want to write this code??
echo $this->Html->script( 'path/to/require', array(
'data-main' => $this->Html->assetUrl( 'path/to/script', array(
'pathPrefix' => JS_URL,
'ext' => '.js'
))
));
this way the data-main attribute will contain the correct url to the js folder.
CakePower supplies a really easy solution to write above code in a compact and simple way:
// With CakePower loaded into your CakePHP app:
echo $this->Html->script( 'path/to/require', array(
'data-main' => 'path/to/script'
));
Easy, isn’t it?