Zend Framework Jutsu with Dojo - Presentation Transcript
Practical Zend Framework Jutsu with Dojo Matthew Weier O'Phinney Software Architect Zend Framework
What is Dojo?
Dojo: Japanese: “ Place of the way” Michael Cornelius, 2008
What does this have to do with JavaScript?
How Dojo Works
DOM manipulation toolset and XHR library
You know, the standard stuff: dojo . query ( "#content a" ). forEach ( function ( node ){ dojo . toggleClass ( node , "decorated" , true ); }); dojo . xhrGet ({ url : "/foo/bar" , handleAs : "text" , load : function ( response , ioArgs ) { dojo . place ( response , "content" ); } });
UI Widgets
Create some content <form method = "post" > Email: <input type = "text" value = "" id = "email" /> <input type = "submit" value = "Send" /> </form>
Sprinkle in some HTML attributes <div class = "tundra" > <form method = "post" dojoType = "dijit.form.Form" > Email: <input type = "text" value = "" id = "email" dojoType = "dijit.form.TextBox" /> <input type = "submit" value = "Send" label = "Send" dojoType = "dijit.form.Button" /> </form> </div>
Inform your document about the Dojo dependencies <style> @import url(/js/dijit/themes/tundra/tundra.css); </style> <script type = "text/javascript" src = "/js/dojo/dojo.js" djConfig = "parseOnLoad:true" ></script> <script type = "text/javascript" > dojo.require( "dijit.form.Form" ); dojo.require( "dijit.form.TextBox" ); dojo.require( "dijit.form.Button" ); </script>
Voila!
Okay, let's look at something a bit more impressive:
What's going on under the hood
dojo.require
It's like PEAR or ZF: /* Load dijit/form/Button.js relative to dojo directory */ dojo . require ( "dijit.form.Button" ); // Load Zend/Form/Element/Button.php from // include_path Zend_Loader :: loadClass ( 'Zend_Form_Element_Button' );
Add addOnLoad events in your view scripts, where they belong $this -> dojo ()-> addOnLoad ( 'function(){ dojo.query(".foo").forEach(function(node){ dojo.attr( node, "dojoType", "dijit.layout.ContentPane"); dojo.parser.parse(node); }); }' );
Define arbitrary javascript to run at page load
Add page-specific JavaScript in your view scripts, where it belongs $this -> dojo ()-> addJavascript ( ' // turn on dijit theme on body dojo.toggleClass( dojo.body(), "tundra", true); ' );
But I hate Dojo markup!
But there are compelling reasons to use declarative markup:
XmlHttpRequests returning markup will need to use declarative markup to work correctly
The W3C specifications allow arbitrary attributes; the validators don't follow the specifications!
The Future Dimitris Agelakis, 2005
Automated builds (or close to it)
Pass your dojo() view helper to a build object, and generate your layer script and build profile $build = new Zend_Dojo_BuildLayer ( 'view' => $view , 'layerName' => 'foo.main' , ); $layerScript = $build -> generateLayerScript (); $profileScript = $build -> generateBuildProfile (); file_put_contents ( 'js/foo/main.js' , $layerScript ); file_put_contents ( 'js/util/buildscripts/profiles/foo.profile.js' , $profileScript );
More comprehensive Dijit support
In closing... knowing one gets the job done; knowing both leads to mastery "AikiDude", 2006
0 comments
Post a comment