Monday, October 1st, 2007

Dojo and the script tags

Category: Dojo, JavaScript

Alex has written about some new script tag support added in Dojo 0.9:

For a long time systems like Flex and Laszlo have had a corner on naturally mixing behavior with markup. HTML’s native <script> tag doesn’t provide any context, and worse, can’t provide a way to be triggered by a particular event or action (outside of proprietary extensions). Several use-cases are important:

  • Scoped execution
  • Event-driven execution
  • Attachment vs. replacement

In Dojo 0.9 we took a hard look at them and devised <script type="dojo/method"> and <script type="dojo/connect">. Lets take a quick tour and show how it makes writing event handlers natural, building new widgets a snap, and over-riding built-in behavior trivial.

So, you can take code like this:

  1. <script type="text/javascript">
  2.    dojo.addOnLoad(function(){
  3.       var cp = dojo.widget.byId("cp");
  4.       dojo.event.connect(cp, "onDownloadStart",
  5.            function(){
  6.                dojo.debug("started downloading:", cp.href);
  7.                // advanced behavior here
  8.            }
  9.        );
  10.  
  11.        // more configuration...
  12.    });
  13. </script>
  14. <div dojoType="dojo:ContentPane" widgetId="cp">
  15. ...
  16. </div>

and replace it with the script tag way of attaching an event:

  1. <div dojoType="dijit.layout.ContentPane">
  2.     <script type="dojo/connect" event="onDownloadStart">
  3.         console.debug("started downloading:", this.href);
  4.         // advanced behavior here
  5.     </script>
  6.     ...
  7. </div>

This definitely looks a little different on the eyes.

Posted by Dion Almaer at 5:43 am
9 Comments

+++--
3.5 rating from 37 votes

9 Comments »

Comments feed TrackBack URI

‘mixing behavior with markup’ – clever but this this goes against the principle of unobtrusive js programming.

Comment by Les — October 1, 2007

Backbase BXML has been doing this
even before “Ajax” was coined.

Comment by Plester — October 1, 2007

Dudes, your site is totally crashed due to not closing some tag or something…

Comment by Thomas Hansen — October 1, 2007

Awfull, horrible, disgusting.

Comment by Laurent V. — October 1, 2007

agrhhh, it would be better if they not remove automatic parsing of scripts included in contentpane resource html

Comment by anonymous — October 1, 2007

This looks more like a step backward than a step forward. Html already has mixing javascript with markup NATIVELY. that’s what we’re trying to get away from, and it is not a horrible limitation of this “new fangled ajax technology that adobe or someone I guess has rolled out on the web”

Comment by Breton — October 1, 2007

moving towards a coldfusion like custom tag library were the tags are the behaviors is not the way to got IMO. A better idea would be to move towards a css like behavior style sheet.

Comment by Chris — October 1, 2007

@all:

Dojo provides a behavior library as part of Core, and you can easily use that to describe how you’d like your nodes to be upgraded, thereby moving all of this logic out of the markup if that’s how you roll. It’s orthoginal to this parser system, and you can use the behavior system to get what you want without ever touching widgets.

That said, markup turns out to be tremendously useful in terms of noting the intended structure. In this case, we’re decorating markup with what we *mean*, which is about as semantic as it gets. It also degrades well and scopes the configuration to the instance. Moving that configuration elsewhere seems to have limited value unless multiple instances could/should share those changes. We’ve considered multiple types of “property” or configuration cascades for instances based on rules, but their application looks to be expensive. They also tend to mirror the prototype chain’s cascade…if you want to change defaults globally, just modify the class prototype. No need for extra syntax.

I am leaning toward a default “mix in values from these objects” property on instances…should be fast enough and space efficient to implement. Thoughts on what that should be called?

Regards

Comment by Alex Russell — October 1, 2007

@Ajaxian
I second Thomas here. Ajaxian tries to kill my CPU. “Dudes!”

@Alex
Good to see that the Dojo people have at least thought about the semantics. Now, if the browser vendors would add solid support for XML namespaces in XHTML documents, then Dojo could keep its information cleanly separated from the HTML, and there would be some order. But until then (if it ever even comes that far), I would personally always choose the Behaviour-approach.

Comment by sb — October 2, 2007

Leave a comment

You must be logged in to post a comment.