-
Notifications
You must be signed in to change notification settings - Fork 1
Usage examples
RequireJS takes some configuration options, and SxRequireJs allows you to set them.
<?php $this->requirejs()->setBaseUrl('newbase'); ?>
#### baseUrl
```php
<?php $this->requirejs()->setBaseUrl('newbase'); ?>
#### Custom configuration
Being able to add configuration to requireJS should be possible. And guess what, it is! This is how you'd add configuration data:
```php
<?php
$this->requireJs()->addConfiguration(array(
'waitSeconds' => 15,
'shim' => array(
'foo' => array(
'deps' => array('bar'),
),
),
));This is especially useful when you don't want to expose the logic of the modules in your views, and just wish to add module paths and applications so you can get going. This helps you separate your javascript modular logic from your views and allows you to not worry about defining applications in "some" view file.
<?php
namespace MyModule;
class Module
{
public function onBootstrap($e)
{
$viewManager = $e->getApplication()->getServiceManager()->get('ViewManager');
$helperManager = $viewManager->getHelperManager();
$requireJS = $helperManager->get('requirejs');
// Adding path as ../ because baseUrl is js/
$requireJS->addPath('somemodule', '../Somemodule/js');
// Add this application with the highest priority (must be loaded first)
$requireJS->addApplication('somemodule/app', 1000);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}This is the same as adding them in the bootstrap.
<?php $this->requirejs()->addPath('somemodule', '../Somemodule/js'); ?>Once you have your Modules and Applications in place, it's time for you to actually put the execution code somewhere.
SxRequireJs offers two methods that will help you with this. Both of them are part of the viewhelper. This is how to use them:
<?php echo $this->requireJs(); ?> <?php echo $this->requireJs()->getRequireJs(); ?>
<?php echo $this->requireJs()->getMain(); ?>
<?php echo $this->requireJs()->getConfig(); ?>Note: They already create their own script tags, so you don't have to put them around these calls again
If you want to fetch the output because you want to do something else with it (like write it to disk, or email it to your friends and family!) you can get it by simply calling render()
<?php $rendered = $this->requirejs()->render(); ?>In some special cases you might want to reset SxRequireJs. This can be done by simply calling clean()
<?php $cleanInstance = $this->requirejs()->clean(); ?>