2009年4月21日星期二

Re: [fw-mvc] Problems with dojo in partials

-- Matthias W. <Matthias.Wangler@e-projecta.com> wrote
(on Monday, 20 April 2009, 11:45 PM -0700):
> I creted a zend framework project with a layout.
>
> My layout looks like this:
>
> <html>
> <head>
> <?php
> $config = Zend_Registry::get('config');
> $baseUrl = Utils::getBaseUrl();
>
> echo $this->headTitle('Title');
> echo $this->headScript();
> echo $this->headStyle();
> echo $this->headLink()
> ->appendStylesheet($baseUrl . '/styles/main.css')
> ->appendStylesheet($baseUrl . '/styles/listMenu.css')
> ->appendStylesheet($baseUrl . '/styles/dropDownMenu.css')
> ->appendStylesheet($baseUrl . '/styles/main/style.css')
> ->appendStylesheet($baseUrl . '/styles/main/iefix.css', 'screen,
> projection', 'lt IE 7')
> ;
>
> if ($this->dojo()->isEnabled()) {
> $this->dojo()
> ->setLocalPath($baseUrl . '/scripts/dojo/dojo/dojo.js')
> ->addStyleSheetModule('dijit.themes.' .
> $config->options->dojo->style)
> /*->requireModule('dijit.Dialog')
> ->requireModule('dojo.parser')
> ->requireModule('dijit.layout.BorderContainer')
> ->requireModule('dijit.layout.ContentPane')
> ->requireModule('dojo.fx')
> ->requireModule('dojo.data.ItemFileReadStore')
> ->requireModule('dojox.data.QueryReadStore')*/
> ->setDjConfig(array('parseOnLoad' => true))
> ;
> echo $this->dojo();
> }
> ?>
> </head>
> <body class="<?=$config->options->dojo->style?>">
> <!-- some HTML... -->
> <?php
> echo $this->partial('header');
> //or echo $this->render('header');
> ?>
> <!-- some HTML... -->
> <?php
> echo $this->layout()->content;
> ?>
> </body>
>
>
> Dojo is working fine in the layout content, but the dojo elements from the
> partial (header.phtml) don't work. I think this is because the partial is
> called after the $this->dojo() has been issued. But how can I fix this
> problem?

This has been asked frequently on the lists to date. The problem you are
seeing is due to the order in which operations are occurring. View and
layout scripts are executed sequentially. In your <head> section, you're
checking to see if dojo is enabled and then configuring it -- but then
*later* actually rendering a script that may enable it. This fails when
the first location where dojo is enabled is your header view script --
as it enables the helper after you've already chosen not to configure
it.

There are two things you should do here. First, configure your dojo()
helper up front in your bootstrap. This will ensure that when it *is*
enabled, it's appropriately setup. You can then disable() it at the same
time.

Second, capture your partial() at the *start* of your layout script, and
echo it later; this will ensure that if you enable dojo within the
partial, the dojo() view helper will then echo correctly. As a quick
example:

<?php $header = $this->partial('header'); ?>
<html>
<head>
...
<?php echo $this->dojo() ?>

The other technique to try is to use declarative Dojo, and to make sure
that dijits used within your layout are registered with the dojo view
helper during bootstrapping. If you always enable Dojo, then you don't
even need to worry about the order in which you do things in your
layout.

--
Matthew Weier O'Phinney
Project Lead | matthew@zend.com
Zend Framework | http://framework.zend.com/

没有评论: