On Wed, Feb 4, 2009 at 11:54 PM, Matthew Weier O'Phinney
<matt...@zend.com>wrote:

> -- HenryG <henry.god...@tanist.co.uk> wrote
> (on Wednesday, 04 February 2009, 04:05 AM -0800):
> > I'm just starting out with Zend and Dojo and I have a quick question
> which I
> > hope you can answer.
> >
> > I have built a tabbed dojo interface and some of the tabs contain dojo
> > forms. I have a view helper which validates the dojo form onsubmit. The
> > helper adds some javascript to head which connects up the submit button.
> >
> > The tabs are linked to an actions which use Context Switching to return
> an
> > ajax response (I guess you'd call it lazy loading). Here's my problem, if
> > I'm trying to lazy load a tab which contains a form then then the
> javascript
> > for the head is not put in place. I cannot for the life of me figure out
> how
> > this should be done. I've searched high and low before posting, so I
> really
> > hope someone out there can help.
>
> This is where digging into the Dojo documentation can help.
>
> With dijit.layout.ContentPane, you can't return javascript; it simply
> won't get executed. However, you *can* embed special script tags within
> the content that the dojo parser will then parse and execute. As an
> example, the following is something I've done:
>
>    <script type="dojo/method" event="onLoad">
>        dojo.connect(dijit.byId("submit"), "onclick", function(e){
>            /* actions go here... */
>        });
>    </script>
>
> Note the "dojo/method" script type -- the parser recognizes various
> dojo/* types and creates callbacks or binds events accordingly.
>
> This can be added to your form via a custom decorator that renders
> inside the ContentPane.
>
> --
> Matthew Weier O'Phinney
> Software Architect       | matt...@zend.com
> Zend Framework           | http://framework.zend.com/
>

Or even better, do what I do, and use the ContentPane's onLoad. This feature
doesn't seem to be documented anywhere I could find, I stumbed across it by
complete fluke, and yet, it is easily the best way to attach JS to tabbed
items in your Dojo + Zend app. Example:

<code>
                       <?= $this->contentPane('files', '',
                                array(    'title' => 'Files',
                                        'class' => 'tab',
                                        'href' => $this->url(
                                            array(    'controller' =>
'files',
                                                    'action' => 'list',
                                                    'format' => 'ajax'
                                            ),
                                'default',
                                true),
                             'parseOnLoad' => true,
                             'onLoad' => "app.attachJavascript('list',
'files')"
                        )); ?>
</code>

attachJavascript in this case is a simple loader, and I pass it through a
couple of variables to let it know the actual JS I want it to load. Then, I
create a function named in a fashion the loader tries to find. If it exists,
it loads it, and there you have it - all the JS you could ever want to
include on a page, all in the right context, and it just works. None of
these special script tags, none of the nonsense with stuff not being able to
attach because the objects don't exist, none of that - it only loads what it
needs to when it needs to, and you can drop in whatever the hell JS you
want. dojo.connect, dojo.query, dojo,subscribe... all works perfectly :D

If the code tags don't work, I apologise, I'm just guessing here. I can
repost if the formatting is too munched.

<code>
dojo.mixin(app, {

        attachJavascript: function (action, controller, params) {
            var functionName = "app." + action + ucfirst(controller) +
"Attach";
            if (eval("typeof " + functionName + " == 'function'")) {
                eval(functionName)(params);
            }
        },

        listFilesAttach: function () {
            dojo.connect(dijit.byId('files_id'), 'onChange', function () {
                dijit.byId('contacts_id').store = new
dojo.data.ItemFileReadStore({ url:
"/contacts/autocomplete/format/ajax?autocomplete=1&files_id=" +
dijit.byId("files_id").value });
            });
        },

}
</code>

Reply via email to