Enable jQuery using with ZendX_JQuery in a view helper

1k views Asked by At

I am trying to activate jQuery through a view helper called within the layout.

The problem is that jQuery is already called within the layout and thus renders the include files, before it is defined in the view helper.

Here are my scripts :

Layout.phtml:

<?php echo $this->doctype(); ?>
<html>
<head>
    <?php echo $this->headTitle() ?>
    <?php echo $this->headLink()->appendStylesheet('/css/base.css') ?>
    <?php echo $this->headMeta() ?>
    <?php echo $this->headStyle() ?>
    <?php echo $this->jQuery() ?>

</head>
<body>
[...]
    <div id="droite" class="column grid_4">
        <!-- Column 2 start -->
        <?php echo $this->render('partials/droite.phtml'); ?>
        <!-- Column 2 end -->
    </div>
</body>
</html>

partials/droite.phtml:

<?=$this->rolelinks(); ?>

My_View_Helper_Rolelinks:

<?php

class My_View_Helper_Rolelinks extends Zend_View_Helper_Abstract
{

    public function rolelinks()
    {
        if (Model_User::hasIdentity()) {
            $role = Model_User::getRole();
            if ($role === 'admin') {
                return $this->view->partial('partials/droite_admin.phtml');
                return;
            }
        } else {
            return '';
        }
    }
}

partials/droite_admin.phtml:

<?php
$this->jQuery()
    ->UiEnable()
    ->addJavascriptFile('/js/jquery.ui.datepicker-fr.js')
    ->addJavascriptFile('/js/onload.js');
?>

<div id="calendar"></div>

So.

Not sure this is the right way to do it, but the main idea is to check if the user is admin to enable jQuery and display a datepicker (calendar).

Thanks in advance for your help.

1

There are 1 answers

1
opHASnoNAME On

You could try to check if jquery is already enabled in your layout to avoid double inclusion.

<head>
    <?php echo $this->headTitle() ?>
    <?php echo $this->headLink()->appendStylesheet('/css/base.css') ?>
    <?php echo $this->headMeta() ?>
    <?php echo $this->headStyle() ?>

    <?php if (! $this->jQuery()->isEnabled()) {
        echo $this->jQuery();
     } ?>
</head>