DailyJS

DailyJS

The JavaScript blog.


Tagtips
Featured

tips feedback

Tips and Links

Posted on .

I've got a bunch of JavaScript-related bookmarks to share. Things that
don't belong in a post by themselves, collected from reader comments and
Twitter.

Unit Testing

Jakub Suder wrote a detailed article on JavaScript unit
testing
.
This was found in a comment on my JavaScript Unit
Testing
article.

Learning Advanced JavaScript

The consensus seemed to be that David Flanagan's Definitive
Guide
is one of the best
books for studying JavaScript, according to comments on Learning
Advanced
JavaScript
.

Firefox 3.6

Firefox 3.6 is out today. There's a
video overview here: What's New in Firefox
3.6
, and full release
notes
:

  • Support for new DOM and HTML5 specifications including the Drag & Drop API and the File API
  • Support for new CSS attributes such as gradients, background sizing, and pointer events
  • Continued support for downloadable web fonts using the new WOFF font format
  • The ability for web developers to indicate that scripts should run asynchronously to speed up page load times

Gordon

Everyone still seems fascinated with the JavaScript Flash renderer,
Gordon. The GitHub repo now has over 1000 followers -- it's one of the most popular JavaScript projects
on GitHub.

Atlas and Cappuccino

I wrote about Atlas in Atlas: Your First
Tutorial
. The beta programme is
still rolling on, and it looks like Cappuccino
is getting closer to version 0.8.

PostageApp

PostageApp is a bulk email service that targets web developers, and has a JSON-based API so it should be easy
for JavaScript developers to use. I saw this in a tweet from
dhh
.

Thanks

We got some positive feedback on our "JavaScript Performance
Rocks!

Featured

jquery events tips ringo

jQuery Custom Events

Posted on .

jQuery makes it easy to bind and trigger custom events. Although it's
not immediately apparent from the
documentation
, creating custom
events is simple and very useful.

Binding Custom Events

Events can be bound to DOM nodes like this:

$("#myElementId").bind("myCustomEvent", function(event) {
  alert("myCustom event happened!");
});

Triggering Custom Events

jQuery's trigger function is
used to instigate the event, passing the name of the event as a string
like this:

$("#myElementId").trigger("myCustomEvent");

If you want to pass data to the handler you can use an event
object
:

var myCustomEvent = jQuery.Event("myCustomEvent");
myCustomEvent.usefulInformation = "some data";
myCustomEvent.otherStuff = "some other data";
$("#myElementId").trigger(event);

Namespaces

Another cool feature of jQuery events is
namespacing, which supports triggering or unbinding specific groups of handlers without having to
reference them directly.

You can add a namespace when binding an event which can be referenced
when you unbind or trigger it. It's possible to use multiple namespaces
at once.

$('.myCSSClass').bind('myEvent.myNamespace.myOtherNamespace', function(){alert('namespaced event')}); 
$('.myCSSClass').trigger('myEvent.myNamespace');
$('.myCSSClass').unbind('myEvent.myOtherNamespace');

If you specify a namespace when unbinding or triggering events, you need
to match one of the bound namespaces (or alternatively, if no namespace
is specified it matches everything).

$('.myCSSClass').bind('myEvent.myNamespace', function(){alert('namespaced event')}); 
$('.myCSSClass').trigger('myEvent.myNamespace'); // mentioned explicitly: will trigger
$('.myCSSClass').trigger('myEvent');  // not specified: will trigger 
$('.myCSSClass').unbind('myEvent.differentNamespace'); // won't unbind