CHAPTER 11

Writing jQuery Plug-Ins

For many developers, the process of creating dynamic websites or AJAX-style applications begins and ends with jQuery. There’s no doubt at all that jQuery is an incredibly useful library, but it’s very easy—and tempting—to simply keep writing procedural-style jQuery calls and do without all of the things that jQuery doesn’t provide the facilities to do.

There’s no need to feel boxed in by jQuery, because right from the start it was designed to be easily extensible. Without touching any of the core code, you can add new API and behaviors to the library, and do so in a maintainable fashion. While this is of course possible in JavaScript, the capabilities provided by CoffeeScript actually make this even more useful.

This chapter looks at the development of jQuery plug-ins with CoffeeScript in both a procedural and object-oriented fashion and how jQuery UI widgets can be created. Either of these frameworks can be used to create new interactive interface elements, but there are some basic differences in how they work and the fundamental architecture.

Anatomy of a jQuery plug-in

When the jQuery library was first published, it was seen as a compatibility library—a kind of shim that developers could use to smooth over the various cracks in different browsers so that they could get on with the real work of developing web applications.

It didn’t take long, though, for other developers to start adding functionality to jQuery, and in fact the first plug-in ...

Get Smashing CoffeeScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.