-
Notifications
You must be signed in to change notification settings - Fork 16
How to use require.JS?
Johan edited this page Jun 6, 2014
·
2 revisions
All require.js files must be wrapped into a require() function:
require(['jquery'], function($) {
// my code
$('document').ready(function() {});
});
Note: never precise the .js extension when you deal with require.js.
- Store your file in a /js/ subfolder located at the root of your app folder:
/my-app/js/script.js - When calling your file from the view with require, prefix it with a
apps!string:$this->assign('require', 'apps!my-app/script');. The file/my-app/js/script.jswill automatically be called
You can call your file directly from the theme by modifying the JS variable require declared in header:
<script type="text/javascript">
var require = {
deps: ['bootstrap', 'apps!my-app/script']
};
</script>
- Wrap your file as described in the first point of this article.
- Modify the JS variable
requiredeclared in header as follow:
<script type="text/javascript">
var require = {
deps: ['bootstrap', 'themes!name-of-my-theme/js/script']
};
</script>
require.js will automatically look for a file located at /themes/name-of-my-theme/js/script.js.