Recently, I’vе bеen working on ѕome fairly Αjax intensive pаges аnd, because I’m working іn a tеam environment, thе nеed аrose for a simple bіt of script to handle аll thе different Αjax “events”. Τhis script needed to bе ѕmall уet еasy to implement
Εnter thе ΥUI. *ϲue Εnter Τhe Dragon thеme*
Ѕo hеre’s a tаsty little script thаt wrаps Αjax requests аnd a ΥUI Custom Εvent іn аn instantiable object thаt workѕ аs аn Αjax DΑO…
(morе…)
Tim,
Thanks for clearing that up. I thought that’s what it was… I should have checked the YUI APIs before posting
Care to share more about the nested CustomEvents? It sounds interesting…
This is great, thanks for your contribution!
I’m what I consider to be a novice with javascript, and OOP in general, so I was having a LOT of trouble figuring out how to fire two separate requests intended to populate two separate divs, using one event. I stumbled across this, and voila! I modified the connect method slightly to allow for passing of ‘targetDiv’, so I can trigger both of the following onLoad:
myAjaxEvent.connect(’groups-test.asp?action=add’,’divDataDisplay’);
myAjaxEvent.connect(’groups-edit-test.asp’,’divDataAddEdit’);
And then replaced the JSON stuff with a simple getElementById(targetDiv).innerHTML = o.responseText
Thanks again!!!
@ Frank: The “ajaxevent” that is passed as the first parameter into the YUI YAHOO.util.CustomEvent constructor is, as you suspect, simply an identifier for the CustomEvent. This string is returned to listeners that receive this event so that they know what event occurred.
I’m not sure you’d need different event names per AjaxEvent object since they will all be of the type “ajaxevent” — it simply identifies the type of event. Of course, there’s no reason why they couldn’t be nested inside other CustomEvents though; for instance, we’ve recently been using them inside a “tabactivated” event inside a tab module.
Tim,
Nice script! I think the example greatly demonstrates the usefulness of this type of event system.
One thing regarding the “subscribe” function. In the YAHOO.util.CustomEvent constructor, you pass “ajaxevent” as the first parameter. Does this mean anything in terms of the application, or is it simply an identifier?
I’m just curious as to whether you would need custom “AjaxEvent” objects that use different event names?
@ Marco: When we first started developing this idea, we originally created AjaxEvent as a singleton. However, it soon became apparent that it needed to be instantiable to maintain several of the properties when handling multiple requests.
I’m not sure that wrapping these instantiated objects inside another singleton would be such a good idea — there are many benefits to be had by allowing those instantiations to be inherited by other objects elsewhere.
As far as recycling eval’d JSON responses is concerned, there’s really no reason why we couldn’t check it against the response within the AjaxEvent object itself. It’s a nice idea and one I shall look into.
Very nice article and code example!
I was wondering whether this whole idea can’t be taken to an even higher level by creating some sort of singleton which holds all of these AJAX events + connections. Like this it might even be possible to recycle eval-ed JSON responses in cases where it’s unlikely that the server response changes between requests to the exact same URL (for example gimmesomejson.php?skeleton=24) if we’d store the response. This could possibly speed up the application and reduce server load.
Not sure if this makes sense, it’s saturday morning after all, but maybe it’s worth looking into?