Use HeadJS for assets loading.
Tested with Silverstripe 3.1
###Setup
If you just want to use the HeadJS integration for your Requirements backend, add the following to your mysite/_config.php
Requirements::set_backend(Injector::inst()->get("HeadJsBackend"));
If you'd like to use the Sectioned Head JS, add the following to your mysite/_config.php
Requirements::set_backend(Injector::inst()->get("SectionedHeadJsBackend"));
###Adding a callback
An example of adding a callback to a CSS or JS requirement is below:
$custom_js = <<<JS
(function ($) {
$(document).ready(function () {
alert("Loaded!");
});
})($);
JS;
Requirements::javascript("framework/thirdparty/jquery/jquery.js");
if(method_exists(Requirements::backend(), "add_callback")){
Requirements::backend()->add_callback("framework/thirdparty/jquery/jquery.js", $custom_js);
}else{
Requirements::customScript($custom_js, "loadedalert");
}
###Adding a dependency
An example of adding a dependency to a CSS or JS requirement is below:
Requirements::javascript("framework/thirdparty/jquery/jquery-ui.js");
if(method_exists(Requirements::backend(), "add_dependency")){
Requirements::backend()->add_dependency("framework/thirdparty/jquery-ui/jquery-ui.js", "framework/thirdparty/jquery/jquery.js");
}
In the example above, we require jQuery UI, but make sure that it is only loaded after jQuery has loaded. Note that we can add multiple dependent files to the jQuery JS file if we want, but there is a limitation around chaining dependencies; i.e. we can only have a single level of dependency - in the example above, we can not add a new file that is dependent on jQuery UI.
###Specifying a section
An example of specifying a section for a CSS or JS requirement is below:
if(method_exists(Requirements::backend(), "add_to_section")){
Requirements::backend()->add_to_section("framework/thirdparty/jquery/jquery.js", SectionedHeadJsBackend::SECTION_AFTER_BODY_OPEN);
}
###Super-specific bugs and their associated fixes
####UserForms module validation won't work (it says it can't find jQuery)
There are lots of different ways we can solve this issue, but the simplest one I've found is if you add an extension to the UserDefinedForm_Controller - or even just edit Page_Controller - and add the following to the init() function:
class UserDefinedFormHeadJSExtension extends Extension {
public function afterGenerateConditionalJavascript(){
if(method_exists(Requirements::backend(), "add_dependency")){
Requirements::backend()->add_dependency("UserFormsConditional", USERFORMS_DIR . '/javascript/UserForm.js');
}
}
}
##Maintainer
Tim - [email protected]
##Original Repo
silverstripe-headjs by the awesome lekoala
Module rating system helping users find modules that are well supported. For more on how the rating system works visit Module standards
Score not correct? Let us know there is a problem