Topic: Is there a way to ask xajax if any functions have been registered?

I like to put xajax right into my framework so the only thing I have to do to use it on a page is register a function or two and I'm off to the races.

The drawback is that I dump out everything that printJavascript dumps out even when there are no functions registered. (Thus it wont be used on that page.)

Is there something I can check before the call to printJavascript so it doesn't dump out any of the javascript if it wont be needed?

Re: Is there a way to ask xajax if any functions have been registered?

This is not currently supported (natively) in xajax... however, I can understand the issue and offer a potential solution (though... I admit, it is a hack)

Just after constructing the $xajax object, use $xajax->getJavascript() to store a copy of the javascript without registered functions added...

In the head of the document, when you are ready to generate the javascript for xajax, again use $xajax->getJavascript() and compare the current version with the one obtained in the beginning...

If they differ, then echo or print the latest version of the script... if they compare equal, then skip it.

Let me know how that works and I'll consider adding some logic to query whether functions have been registered.

// Joe

xajax Developer
Connect to me on LinkedIn:
http://www.linkedin.com/in/calledtoconstruct

3 (edited by vita10gy 2008-01-30 9:04:27 PM)

Re: Is there a way to ask xajax if any functions have been registered?

That would be possible, but I think it would be a valuable (and probably easy) addition to add natively.

I've never poured through the source, but I assume that the functions and or events are stored in an array that you would just need a function to return the count of, or count > 0, or, for that matter, return the array, or 3 functions for all the above.

I was envisioning something along the lines of:

if($xajax->hasRegisteredFunctions())
{
   $xajax->printJavascript();
}

Edit: Should I take this to feature request area to make it an official request?

Re: Is there a way to ask xajax if any functions have been registered?

Well, please understand, xajax supports plugins (for both the request and response ends of the transaction)... so, while it sounds simple, it is not that easy; but certainly not impossible.

A plugin could be written to always insert a registered function (so every page would have that function available)... if the plugin author so desired.

However, I can see an addition to the plugin architecture, whereby we could query to see if a plugin will produce javascript output.  $plugin->willOutputScript() or $plugin->hasScriptOutput()... but then we'd need $plugin->willOutputStyle() or $plugin->hasStyleOutput() as well.

I'll move the topic over to feature requests. smile

// Joe

xajax Developer
Connect to me on LinkedIn:
http://www.linkedin.com/in/calledtoconstruct

Re: Is there a way to ask xajax if any functions have been registered?

If you were using a plugin that always put something in the functions then hasRegisteredFunctions would be true.

You could call that function, so you'd want that output, right?

I just want to know if the "default" javascript will be dumped out or if the page has some functions it has the potential to use.