Skip to content
RWOverdijk edited this page Jul 28, 2012 · 27 revisions

Adding module paths and applications on bootstrap

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');

        $requireJS->addPath('somemodule', '../Somemodule/js');
        $requireJS->addApplication('somemodule/app', 1000); // Highest priority
    }

    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__,
                ),
            ),
        );
    }
}

Getting the output

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:

__toString()

<?php echo $this->requireJs(); ?>

getRequireJs(), getConfig(), getMain()

    <?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

Adding 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
$this->requireJs()->addConfiguration(array(
    'waitSeconds' => 15,
    'shim' => array(
        'foo' => array(
            'deps' => array('bar'),
        ),
    ),
));

Clone this wiki locally