Correct Usage of YUI

Correct Usage of YUI

Mat Cannings -
回帖数:2
I am wanting to use YUI within Moodle but have been unable to find information regarding the correct process within Moodle. I have had look around the documentation and forums but not found too much information available.

From reading a couple of books about AJAX and graceful degradation the correct way to implement any AJAX is to (very simply)
  1. Check for availability of Javascript
  2. Have a page that works fine without Javascript being enabled
  3. Once a page has loaded, through Javascript if available, update and insert extra functionality
Are there standard functions/variables existing that should be used to check for YUI usage/availability. Is there a 'queue' that should be used to add functions to that will be processed once a page has loaded.

回复Mat Cannings

Re: Correct Usage of YUI

sam marshall -
Core developers的头像 Peer reviewers的头像 Plugin developers的头像
Hi Matt,

Please search the code for uses of the require_js function. This is the function you should call to load YUI libraries.

Beyond that I am not sure as to the answers for your questions. Sorry. I've used YUI for AJAX so I know how to do that bit 微笑 Haven't done drag and drop (with YUI) yet, maybe in a couple days.

One other note is that if you don't want distracting page flicker it can sometimes be better to update things before the page finishes loading. For example if you are going to do something to a div and you don't want the user to see it change, you could do e.g.

<div id="whatever">non-JS or too-old JS version of contents</div>
<script type="text/javascript">
setupDiv('whatever');
</script>

By this method you can obviously only refer to page elements above the script, and it does clutter the code a bit. But I often find it useful and it avoids difficulty with onload chains. This is not to say that you shouldn't use onload, of course that is fine too. I use both techniques.

--sam
回复sam marshall

Re: Correct Usage of YUI

Mat Cannings -
Thanks Sam,
Unfortunately I have previously read a great book called DomScripting (http://domscripting.com/book/) which covers Javascript/Ajax but from a d.i.y. perspective and so I am having to 'unlearn' that to switch to using an AJAX library.
I had searched through Moodle and not found anything but have since searched through YUI documentation and I think that handles all the 'queueing' and page updating as part of it's libraries. Moodle has an ajaxenabled setting stored in $CFG but from first glance it appears to relate to the option to enable Ajax rather than any sort of AJAX/Browser capability testing.
The YUI Menu block is the only use of YUI that I have a had a look at but have not yet had a chance to properly disect, it does seem to do it's own browser capability checks.
I will start to go through the YUI examples and see where I get.