LogoPhly, boy, phly
the weblog and site of Matthew Weier O'Phinney

Wednesday, May 21. 2008

Zend Framework Dojo Integration

I'm pleased to announce that Zend Framework will be partnering with Dojo Toolkit to deliver out-of-the-box Ajax and rich user interfaces for sites developed in Zend Framework.

First off, for those ZF users who are using other Javascript toolkits: Zend Framework will continue to be basically JS toolkit agnostic. You will still be able to use whatever toolkit you want with ZF applications. ZF will simply be shipping Dojo so that users have a toolkit by default. Several points of integration have been defined, and my hope is that these can be used as a blueprint for community contributions relating to other javascript frameworks. In the meantime, developers choosing to use Dojo will have a rich set of components and integration points to work with.

The integration points we have defined for our initial release are as follows:

  • JSON-RPC Server: We are re-working the Zend_Json_Server that has been in our incubator since, oh, what? 0.2.0? and never released to actually follow a specification: JSON-RPC. This will allow it to work seamlessly with Dojo, as well as other toolkits that have JSON-RPC client implementations. I have actually completed work on this, though the proposal is waiting to be approved; if you want to check it out, you can find it in the ZF svn.

    The original Zend_Json_Server implementation will be abandoned. It was never fully tested nor fully documented, which has prevented its release. Additionally, since it implemented its own ad-hoc standard, it did not provide the type of interoperability that a true JSON-RPC server implementation will provide. I am excited that we will finally be able to provide a standards-compliant solution for general availability.

    One final note: there are currently two different JSON-RPC specifications, 1.0 and 2.0. Currently, the implementation I've been working on will switch payload formats based on the request, and can deliver different SMD formats appropriately as well.

  • dojo() View Helper: Enabling Dojo for a page is not typically as trivial as just loading the dojo.js script -- you have a choice of loading it from the AOL CDN or a local path, and also may want or need to load additional dojo, dijit, or dojox modules, specify custom modules and paths, specify code to run at onLoad(), and specify stylesheets for decorating dijits. On top of this, this information may change from page to page, and may only be needed for a subset of pages. The dojo() view helper will act as a placeholder implementation, and facilitate all of the above tasks, as well as take care of rendering the necessary style and script elements in your page.
  • Form Element implementations: One area that developers really leverage javascript and ajax toolkits is forms. In particular, many types of form input can benefit from advanced and rich user interfaces that only javascript can provide: calendar choosers, time selectors, etc. Additionally, many like to use client-side validation in order to provide instantaneous validation feedback to users (instead of requiring a round-trip to the server). We will be identifying a small group of form elements that we feel solve the most relevant use cases, and write Dojo-specific versions that can be utilized with Zend_Form. (One thing to note: Zend_Form's design already works very well with Dojo, allowing many widgets and client-side validations to be created by simply setting the appropriate element attributes.)
  • dojo.data Compatibility: dojo.data defines a standard storage interface; services providing data in this format can then be consumed by a variety of Dojo facilities to provide highly flexible and dynamic content for your user interfaces. We will be building a component that will create dojo.data compatible payloads with which to respond to XmlHttpRequests; you will simply need to pass in the data, and provide metadata regarding it.

So, some examples are in order. First off, Zend_Json_Server operates like all of ZF's server components: if follows the SoapServer API. This allows you to attach arbitrary classes and functions to the server component. Additionally, it can build a Service Mapping Description (SMD) that Dojo can consume in order to discover valid methods and signatures. As an example, on the server side you could have the following:


// /json-rpc.php
// Assumes you have a class 'Foo' with methods 'bar' and 'baz':
$server = new Zend_Json_Server();
$server->setClass('Foo')
       ->setTarget('/json-rpc.php')
       ->setEnvelope('JSON-RPC-1.0')
       ->setDojoCompatible(true);

// For GET requests, simply return the service map
if ('GET' == $_SERVER['REQUEST_METHOD']) {
    $smd = $server->getServiceMap();
    header('Content-Type: application/json');
    echo $smd;
    exit;
}

$server->handle();
 

On your view script side, you might then do the following:


<h2>Dojo JSON-RPC Demo</h2>
<input name="foo" type="button" value="Demo" onClick="demoRpc()"/>
<?
$this->dojo()->setLocalPath('/js/dojo/dojo.js')
             ->addStyleSheetModule('dijit.themes.tundra')
             ->requireModule('dojo.rpc.JsonService');
$this->headScript()->captureStart(); ?>
function demoRpc()
{
    var myObject = new dojo.rpc.JsonService('/json-rpc.php');
    console.log(myObject.bar());
}
<? $this->headScript()->captureEnd() ?>
 

And, finally, in your layout script, you might have the following:


<?= $this->doctype() ?>
<html>
    <head>
        <title>...</title>
        <?= $this->dojo() ?>
        <?= $this->headScript() ?>
    </head>
    <body class="tundra">
        <?= $this->layout()->content ?>
    </body>
</html>
 

The example doesn't do much -- it simply logs the results of the JSON-RPC call to the console -- but it demonstrates a number of things:

  • dojo() View Helper: The example shows using dojo from a local path relative to the server's document root; using the 'Tundra' stylesheet shipped with Dojo and attaching it to the layout; capturing a required module ('dojo.rpc.JsonService'); and rendering the necessary Dojo stylsheet and script includes in the layout.
  • JSON-RPC client: Dojo requires that you point the JsonService to an endpoint that delivers a Service Mapping Description; in this example, I use any GET request to return the SMD. Once the SMD is retrieved, any methods exposed are available to the Javascript object as if they were internal methods -- hence myObject.bar(). Dojo's current implementation performs all other requests as POST requests, passing the data via the raw POST body.

There will be more to come in the future, and I will be blogging about developments as I get more proposals up and code into the repository. All in all, this is a very exciting collaboration, and should help provide ZF developers the ability to rapidly create web applications with rich, dynamic user interfaces.

Update: Andi has posted an FAQ on our integration.

Posted by Matthew Weier O'Phinney in PHP at 10:57 | Comments (25) | Trackbacks (0)
Defined tags for this entry: dojo, mvc, php, zend framework
Related entries by tags:
Speaking at php|tek
Creating composite elements
Speaking at DPC (again!)
Rendering Zend_Form decorators individually
Zend Framework 1.8 PREVIEW Release

Trackbacks
Trackback specific URI for this entry

No Trackbacks

Comments
Display comments as (Linear | Threaded)

The timing couldn't be much better for you to choose a JS lib. At work, we were just about to make a decision on what lib to go for in an upcoming project, and now it's pretty much obvious. Can't wait to try it out :-)

Any estimates on when the new components will reach incubator/core?
#1 Robin Skoglund on 2008-05-21 11:50 (Reply)
The JSON-RPC server is in a branch currently and will be moved to the incubator within the next week; it is fully unit tested as well as tested functionally with Dojo.

The dojo view helper is about 75% complete, and I will be posting it in a branch pending the completion of the proposal process (I can finally post a proposal now that we've broken silence ;-) ).

The remainder will be done likely in the next 6 weeks (I have a lot of travelling in the next three weeks for work and conferences).
#1.1 Matthew Weier O'Phinney (Link) on 2008-05-21 13:11 (Reply)
Congrats matthew! Looking forward to this integration.
#2 Ivo Jansch (Link) on 2008-05-21 12:00 (Reply)
Thanks, Ivo -- it's been a long time coming, but the steps we've made the past couple months have been very big and promising. It's a very exciting partnership!
#2.1 Matthew Weier O'Phinney (Link) on 2008-05-21 13:12 (Reply)
Hey Matthew, thanks for your work on ZF (and Zend_Form in particular), as well as on the great tutorial input here and on DevZone. I assumed the JS lib integration would end up with Dojo, being BSD-licensed, and so close in nature. One of the things that kept me from using Dojo widgets was and is the fact that they invalidate XHTML. (Hello World!). The issue is discussed every now and then on the Dojo forums, but I didn't find any "official" statements on the problem yet. So, I'm not sure if Andi's "Dojo not only implements published standards, but also drives them." is entirely correct. Being concerned about valid code, I am generally taking great care to use unobstrusive techniques in adding javascript functionalities, and I do hope that the ZF Dojo integration will follow, or at least allow for ways of programmatically building widgets rather than requiring to add invalid attributes to (X)HTML code.
Bernd
#3 Bernd Matzner (Link) on 2008-05-21 13:42 (Reply)
I meant to add example code where it says "Hello World", which obviously was stripped out. I was referring to the dojoType or widgetId attributes etc.
#3.1 Bernd Matzner (Link) on 2008-05-21 13:45 (Reply)
If validation is a concern, Dojo allows you to do everything programmatically already, and I have done so myself in several projects.

Attributes like dojoType and widgetId are merely for convenience; it's easier to associate the behavior with the actual element than it is to write a bunch of javascript that decorates the page. The fact of the matter is that the methodology works in every major browser already, regardless of whether or not it validates; as such, it's a pragmatic approach.

Some links for discussion points:

http://alex.dojotoolkit.org/?p=622

http://alex.dojotoolkit.org/?p=642

Besides this, however, the standards I was primarily pointing at and that Andi references are things like Dojo's commitment to JSON-RPC, JSON-Schema, Bayeux, etc. They are working hard to help define Javascript standards to make it easier for developers to code UIs and server-side listeners. Imagine if you could write a single JSON-RPC server and swap out your client side with a new JS library without needing to change anything server side; this would be a huge gain.
#3.2 Matthew Weier O'Phinney (Link) on 2008-05-21 15:07 (Reply)
Thanks for the clarification, but one question stays:

How will the dojo helper work? Is there an option to tell them "do that programmatically" to keep the output valid? This is my biggest concern with Dojo.

Guess I'll have a look at the code ;-)
#3.2.1 Robin Mehner on 2008-05-22 08:23 (Reply)
I'll be posting the proposal today or tomorrow, and will update this blog entry with a link to it.

The dojo() view helper simply sets up the various script/style includes, as well as any dojo.require statements and custom module paths. The part you're interested in will be with the form elements, for which there is no proposal yet, and for which I have not yet created any functionality. I think that programmatic creation would definitely be achievable, and I'll list it as a 'SHOULD' portion of the proposal when I create it.
#3.2.1.1 Matthew Weier O'Phinney (Link) on 2008-05-22 08:44 (Reply)
Hmm... I need to ship jQuery too, but I suppose two Js frameworks won't hurt ;-)

I'm just wondering: will those Dojo Form Elements also degrade nicely to plain HTML? If so, that would be excellent :-)
#4 Vincent on 2008-05-21 15:46 (Reply)
The dojo form elements will simply be automating some of the logic -- they will make use of the dojo() form helper to ensure the appropriate dojo.require statements are made, and will then inject the appropriate dojoType attribute. This means, simply, that yes, they _will_ degrade gracefully, just as Dojo does currently.
#4.1 Matthew Weier O'Phinney (Link) on 2008-05-21 15:59 (Reply)
Great! (Sorry, I have no experience at all with Dojo so I didn't know...)
#4.1.1 Vincent on 2008-05-21 17:36 (Reply)
The very nature of jQuery, and its ability to be abstracted from html is one of its true beauties. I think its integration into the ZF will be much easier than Dojo from what I've seen.
#4.2 Jay M. Keith on 2008-05-24 12:53 (Reply)
I know this may be hard to believe, but I've found integrating _any_ JS library into ZF pretty trivial. Much of jQuery's simplicity has a direct correlation in dojo.query(), btw.

Additionally, the idea is that the Dojo integration will serve as a blueprint for community contributors to submit other JS library integration layers. Please stay tuned!
#4.2.1 Matthew Weier O'Phinney (Link) on 2008-05-25 12:37 (Reply)
Would be great something like this for jQuery, jQuey is a great toolkit, lots of plugins, strong community, will be awesome.

, Congrats :-)
#5 Juan Felipe Alvarez Saldarriaga on 2008-05-21 17:24 (Reply)
I wish it were jQuery instead. Dojo, while being very powerful is one of the most complicated Javascript libraries around. Hopefully you'll consider other javascript framework helpers (or maybe I should submit a proposal? ;-) ).
#6 Eran Galperin (Link) on 2008-05-21 20:09 (Reply)
We fully expected that regardless of the library we chose, we would not make everyone happy -- so, to that end, as I noted in the entry, we are a) making everything use-at-will, and b) expecting and encouraging community contributions of other javascript integration components. While we will not ship other libraries, we will ship the integration components. The intention is that the dojo integration will serve as a blueprint for such contributions.

And yes, proposals are always accepted. ;-)
#6.1 Matthew Weier O'Phinney (Link) on 2008-05-21 21:04 (Reply)
First, thanks for taking the time to comment on this.

I'm *very* biased (I work on Dojo itself), but Dojo isn't any more complicated than it is deep, which is to say, you only "pay" the complexity cost for the parts that you actually use. If you want to use Dojo in the exact same style as you use JQuery, you can do that (via dojo.query())....but Dojo picks up where JQuery leaves in the lurch by providing tools, modules, and support for building larger-scale interactions, optimizing for deployment, and handling internationalization, localization, and accessibility. Of course, you never hit those APIs unless you're doing something that requires them, and often not even then.

As JQuery grows in scope, we expect to see them continuing to borrow many of the innovative features currently found in Dojo as the need to solve the same problems becomes as pressing for them as it was for Dojo when Dojo was pressed into service on large-scale sites like AOL Mail. That's not by way of saying that JQuery is bad, just to suggest that complexity is relative to the problem set you're trying to approach. Dojo gives you tools to attack a much larger range of problems...hence the depth and breadth of the toolkit.

Regards
#6.2 Alex Russell (Link) on 2008-05-24 17:23 (Reply)
I'm really disappointed, other frameworks that I'll won't name have better documentation, community and really better support. Dojo really lacks documentation. Without good docs, code examples, strong community and a good API browser (like that of ZF) I'll go elsewhere. I hope ZF will stay JS independent.
#7 Daniel Stoyanov (Link) on 2008-05-24 17:27 (Reply)
Dojo's documentation has improved vastly in the past several months, and projects like DojoCampus (http://dojocampus.org/) are providing concrete examples for developers to utilize to achieve complex effects (and trivial ones, as well). Additionally, Dojo now has API docs online (http://api.dojotoolkit.org/).

ZF _will_ remain JS library independent, in that we will not be integrating Dojo in such a way that Dojo is the only option; the idea is that the Dojo integration will serve as a blueprint for other JS library integration components. Dojo _will_ be the only JS library shipped with ZF for the forseeable future, however.
#7.1 Matthew Weier O'Phinney (Link) on 2008-05-25 12:40 (Reply)
Hey I tried the URL you provided with your branch http://framework.zend.com/svn/framework/branch/user/matthew/zed_json_server and I got a 404 error page...
Is this the right URL?
I have to try your new library ;-)
#8 Christian Sanchez A. on 2008-06-28 21:30 (Reply)
The JSON-RPC server is now in the standard incubator:

http://framework.zend.com/svn/framework/standard/incubator

as the component 'Zend_Json_Server'; it will be promoted into the standard library in the coming weeks as it is fully documented and tested now.
#8.1 Matthew Weier O'Phinney (Link) on 2008-06-29 11:52 (Reply)
I 've some feedback fro your Zend_Dojo_View helper...

When i was trying to use your helper the following error came up:

Warning: preg_replace() [function.preg-replace]: Compilation failed: missing terminating ] for character class at offset 22 in /.../Zend/Dojo/View/Helper/Dojo/Container.php on line 504

I did some research on regular expressions and found out that the line:
$localPath = preg_replace('|dojo[/\\]dojo.js[^/\\]*$|i', '', $localPath);

must be replaced with:
$localPath = preg_replace('|dojo[/\\\\]dojo.js[^/\\\\]*$|i', '', $localPath);
#9 Christian Sanchez A on 2008-07-04 16:31 (Reply)
Thanks for the report; I've fixed this in svn now.

In the future, please direct such comments to the ZF mailing lists. :-)
#9.1 Matthew Weier O'Phinney (Link) on 2008-07-04 16:41 (Reply)
Is the Menu style in DoJo, I checked in there site, I could not find any Script.
#10 Outsourcing India (Link) on 2009-02-17 07:06 (Reply)

Add Comment

Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

 
 
  • Home
  • Resume
  • Blog
  • Phly PEAR Channel
  • Contact Me
  • About this site

ZCE

Zend Education Advisory Board Member

Add to Technorati Favorites

Calendar

Back July '09
Mon Tue Wed Thu Fri Sat Sun
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

Quicksearch

Links

  • PHLY - PHp LibrarY
  • Paul M. Jones
  • Mike Naberezny
  • Shahar Evron
  • Planet PHP
  • Zend Where I now work
  • Garden.org Where I once worked

Archives

July 2009
June 2009
May 2009
Recent...
Older...

Categories

XML Linux
XML Personal
XML Aikido
XML Family
XML Programming
XML Dojo
XML Perl
XML PHP

All categories

Syndicate This Blog

XML RSS 0.91 feed
XML RSS 1.0 feed
XML RSS 2.0 feed
ATOM/XML ATOM 0.3 feed
ATOM/XML ATOM 1.0 feed
XML RSS 2.0 Comments

Show tagged entries

xml best practices
xml books
xml conferences
xml decorators
xml dojo
xml dpc08
xml file_fortune
xml linux
xml mvc
xml oop
xml pear
xml perl
xml personal
xml php
xml phpworks08
xml programming
xml ubuntu
xml vim
xml webinar
xml zendcon
xml zendcon08
xml zend framework
© 2004 - present, Matthew Weier O'Phinney
matthew-web <at> weierophinney.net