Close
Loading, please wait...
This announcement will hide in 60 seconds

YUI Plugin: Dispatcher - Documentation

2007-05-26
RSS Channel bookmark this link

Simple Use Case: YAHOO.plugin.Dispatcher

Markup (dispatcher container element):
<div id="mydynamicarea">
</div>

Script:
YAHOO.plugin.Dispatcher.fetch ( 'mydynamicarea', 'PATH/TO/CONTENT/page.html' );

The Dispatcher Manager will fetch the remote page, and set the response as the content of the container area "mydynamicarea".

Singleton: YAHOO.plugin.Dispatcher

YAHOO.plugin.Dispatcher.fetch(str|HTMLElement|obj el, str uri, obj config);
Arguments:
(1) HTML ID or HTMLElement of the container.
(2) The remote file used by the connection manager.
(3) Configuration Object: JS object defining configuration properties for the area. See Configuration section for full list.

YAHOO.plugin.Dispatcher.delegate(obj tab, obj tabview, obj config);
Arguments:
(1) reference to the new tab.
(2) reference to the tabview (optional).
(3) Configuration Object: JS object defining configuration properties for the area. See Configuration section for full list

YAHOO.plugin.Dispatcher.process(str|HTMLElement|obj el, str content, obj config);
Arguments:
(1) HTML ID or HTMLElement of the container.
(2) content: You can use another component to managemente the ajax process, and pass the response directly to the dispatcher to process the content and put it inside the container.
(3) config: Configuration Object: JS object defining configuration properties for the area. See Configuration section for full list

YAHOO.plugin.Dispatcher.cssLoader(str uri, obj config);
Arguments:
(1) The remote JS file used by the connection manager.
(2) config: Configuration Object: JS object defining configuration properties for the JS file. See Configuration section for full list

YAHOO.plugin.Dispatcher.jsLoader(str uri, obj config);
Arguments:
(1) The remote CSS file used by the connection manager.
(2) config: Configuration Object: JS object defining configuration properties for the CSS file. See Configuration section for full list

YAHOO.plugin.Dispatcher.applyCSS(str cssCode, obj params, obj config);
Arguments:
(1) cssCode: a chunk of CSS code.
(2) params: List of properties for the new CSS tag (media, href, etc).
(3) config: Configuration Object: JS object defining configuration properties for the CSS chunk. See Configuration section for full list

Solutions:

Use the dispatcher to process the tabview connection method

var tabView = new YAHOO.widget.TabView({id: 'demo'});
YAHOO.plugin.Dispatcher.delegate (new YAHOO.widget.Tab({   label: 'Remote Scripts',   dataSrc: 'PATH/TO/xhtml.with.remote.scripts.html',   cacheData: true,   active: true }), tabView);
Use the dispatcher to load a remote uri inside a DIV:

 YAHOO.plugin.Dispatcher.fetch ( 'my_dynamic_area', 'PATH/TO/xhtml.with.remote.scripts.html' );

Use the dispatcher to process the content provided by an external package:

var area = YAHOO.util.Dom.get ('dynamic_area');
YAHOO.plugin.Dispatcher.process ( area, 'xhtml content here...' );

CSS Injection:

// injecting a remote CSS file in your page...

YAHOO.plugin.Dispatcher.cssLoader( 'css-injection-default.css');

// injecting certains CSS's rules in your page...
YAHOO.plugin.Dispatcher.applyCSS ("#demo {border: 1px solid #ff0000;} #demo ul {text-align: center;}");

Avoid memory leaks:

Define the rules to destroy the controls/widgets before change the content of the areas. In this example the dispatcher will destroy the DataTable control, releasing the memory and then will display the new content. Also, by default, the dispatcher will release all the listeners defined for elements within a area before display the new content (YAHOO.util.Event.purgeElement).

// setting the destruction's rules from inside (use this sentence at the end of the remote content)
YAHOO.plugin.Dispatcher.destroyer.subscribe (function(el, config){
myDataTable.destroy ();
}); // setting the destruction's rules from outside (use this senctence at any time) YAHOO.plugin.Dispatcher.onDestroy = function ( areaID, function(el, config){
myDataTable.destroy ();
}, scope);

Key Interesting Moments in Dispatcher

Events Fires Arguments Execution Scope
onStart ...before start the loading or the process methods [0] element: container DOM ref config: Configuration Object
before Deprecated in favor of onStart... [0] element: container DOM ref config: Configuration Object
onReady ...after display the new content and before start the execution process [0] element: container DOM ref config: Configuration Object
onLoad ...after finish the execution process [0] element: container DOM ref config: Configuration Object
after Deprecated in favor of onLoad... [0] element: container DOM ref config: Configuration Object
onDestroy ...before display the new content [0] element: container DOM ref config: Configuration Object

Key Dispatcher Configuration Options

Option (type) Default Description
action (s) 'replace' sustitution method (replace | update | tabview)
scope (o) window Object used for correction of the execution scope for remote scripts.
onStart (f) null Deafult function for the corresponding moment
onLoad (f) null Default function for the corresponding moment
onDestroy (f) null Default function for the corresponding moment
relative (b) false If the path of the remote assets (CSS, Images and JS files) is relative or not.
baseURI (s) null The base URI for relative assets. (by default the baseURI is the path to the remote content)

Dependencies

Requires:
YUI:YAHOO, YUI:Dom, YUI:Event and YUI:Connection

Facts

- The dispatcher simulate the browser execution (ordered execution)
- The dispatcher allow cross reference within the dynamic area (YAHOO.util.Dom.get ('id_of_element_within_container'))
- The dispatcher use lineal execution (one thread)

Firewall:

Before load the content of an external source, the dispatcher will analyze the URI, at this point you can define your own routine to determine if the source can be loaded or not, defining your own policies.

In this case we can use the configuration object to define this policies:

  • proxy (string):
    It's an URL that will be used to load external sources thru a proxy, by default the proxy setting is:
    '/dispatcher.php?uri='
  • monolithic (bool)
    It's an option parameter, if true the dispatcher will be forced to complied with the cross domain policy, loading only the files from the same domain.
  • firewall (function):
    It's an optional function that will override the proxy and the monolithic parameters, and will be called it using the URI as the first argument, and using the configuration object like the scope, and the result of this function will be used like the correct URI; if the result isn't an string, this file will be discarded.

Example:

YAHOO.plugin.Dispatcher.fetch ( 'my-div', '/PATH/TO/my-url.html', {
  proxy:  '/PATH/TO/my-proxy.php?f='
});
YAHOO.plugin.Dispatcher.fetch  ( 'my-div', '/PATH/TO/my-url.html', {
  firewall:  function(uri){
    // transform the uri...
    return  uri;
  }
});

2 Comments

Events

I see the events onStart, onLoad and onDestroy. But i'm asking what are the exact moments dispatcher fires these events?

Btw, the dispatcher is a great tool to load and savely execute js code dynamicaly. It should be part of the YUI Library.

Comment by Bernhard Mayer - November 5, 2008

RE: Events

Hello Bernhard, thanks for the kind words.

About the events, these are not Custom Events, they are just hook methods (functions defined in the configuration object), and they will be executed at certain moments:

1. onStart: immediately after starting the AJAX request to the server.
2. onLoad: after the whole process, which mean after inserting the content within the DIV, and executing the scripts as well.
3. onDestroy: just before replacing the content of the DIV for the new content.

Makes sense for you?

About including the Dispatcher as part of the YUI library, I don't think we will include it in the HEAD branch, it's like an advanced plugin.

Comment by Caridy - November 5, 2008
Latest Releases

Accordion Manager
This YUI Widget will allow you to create expandable and collapsible areas using the correct HTML and CSS, but without writing a single line of javascript code. (new)

Loading Mask
The Loading Mask widget will hide all the page's content until the onLoad's event, which mean that the website will not be displayed until the current page be fully loaded. Also this widget can be used in conjunction with the YUI Connection Manager to block the actions over the page until the AJAX's request finish. (new)

 
Hot Components

Accordion Manager
Create expandable and collapsible areas using the correct HTML and CSS, but without writing a single line of javascript code. (released)

Bubbling Core
The bubbling core now include a generic communication mechanism for widget2widget, widget2browser and widget2server messages. (updated lately)

Tooltip Manager
Improved, now using the YUI Overlay instead YUI Tooltips, also introduce a new feature, "content onDemand" using the YUI connection manager. (updated lately)

 
Coming Soon

Bubbling Library for YUI 3.x
A new version of the Bubbling Library compatible with the NEW YUI 3.x. (comming soon)

 
Recommended Reading

Using the YUI TabView Control with the Dispatcher Plugin
YUI Dispatcher Plugin and how to use it along with the YUI TabView to load on-demand content using the YUI Connection Manager.

The Bubbling Technique & Custom Event, YUI’s Secret Weapon
In this article I’ll share my experiences in the event-driven programming within the web browser and show how the Bubbling Library, combined with YUI’s Custom Event capabilities, can create an unobtrusive behavioral layer suitable for powerful web applications.

Event-Driven Web Application Design
I recommend you to read carefully the Christian Heilmann's paper, where he wrote about how to plan an Event-Driven application, and how this technique will become the future of the web application as a result of an evolutive process.

A JavaScript Module Pattern
Eric Miraglia's detailed explanation of how to use the closures and specifically the module pattern (singlenton) as a flexible and multi-funcional structure...

 
Related Links

Yahoo! UI Library (YUI)
YUI Official Website at the Yahoo! Developer Network.

YUI Official Forum
Get help and share your knowledges thru the frenetic javascript mailing list at Yahoo! Developer Network...

Motionbox EventHandler
For those who use propotype instead YUI, here is an implementation of the bubbling core routine for Propotype, I never tested it, but it's seen like a good implementation.

 
 
Linux MySql Server PHP Mozilla FireFox FireBug YAHOO! Bubbling Library
This website is XHTML 1.0 valid! This website can be browsing with all browsers! This website is valid for Level A of Web Accessibility!