[development] Drupal way of doing onchange

Jamie Holly hovercrafter at earthlink.net
Sun Nov 22 23:14:43 UTC 2009


Unless it is added inline with a defer then you do risk loading problems:

<script type="text/javascript">
    $("#edit-cc").change(edit-cc-function);
</script>
<script src="/path/to/edit-cc-function.js"></script>

Without the defer, edit-cc-funciton will be undefined.

Even though this scenario is most likely for private use, something else 
that should be considered in contrib modules as well as core (and 
possibly added to the JS coding standards) is the use of event 
namespaces. That way you can easily disable any bound events later on in 
the code without having to rewrite the original JS. Something like:

$("#edit-cc").bind("click.myccmodule",edit-cc-function);

Now if I want to unbind that function all I got to do is:


$("#edit-cc").unbind("click.myccmodule");

Want to unbind every event that module binds to?

$("#edit-cc").unbind(".myccmodule");

It really adds a nice new layer of extensibility.

http://docs.jquery.com/Namespaced_Events

Jamie Holly
http://www.intoxination.net 
http://www.hollyit.net



Scott Reynen wrote:
> On Nov 22, 2009, at 2:25 PM, Greg Holsclaw wrote:
>
> > Of course, from a JavaScript purists mindset, you should attach all  
> > the
> > event handlings later, but that is a completely different topic.
>
>
> I'll bite.  One might argue that inline JS violates Drupal coding  
> standards:
>
> http://drupal.org/node/172169#jscodeplacement
>
> That could be read with a more narrow meaning (i.e. event function  
> bindings are okay, nothing else), but is there any actual benefit to  
> doing inline JS?  It's not as if it's more difficult to select an  
> element and apply an event handler in jQuery.
>
> $('#edit-cc').change(some_js_funtion);
>
> Doing things the "purist" (right) way seems to be both easier and  
> better for modularity, so why not do it?
>
> --
> Scott Reynen
> MakeDataMakeSense.com
>
>
>
>   


More information about the development mailing list