Doing It The jQuery Way · Sep 5, 02:50 PM by Dylan Doxey
Here's (a slimmed version of) one of the more intersting bits of JavaScript code I've written recently.
var refresh_ui = function() {
$('#keyword_select').load('/keywords?action=list');
}
var save = function() {
var item_id = $('#item_select').val();
var keywords = $('#keyword_select').val();
return $.post( '/settings/keywords',
{
action : 'save',
item_id : item_id,
keywords : keywords,
},
refresh_ui
);
};
$('#save_button').click(save);
This snippet illustrates several ways of doing things using jQuery that break from the old norm:
- defining anonymous functions vs. named functions
- describing data structures with inline JSON vs. building associative arrays
- getting items with CSS selectors vs. getElementById (or something even more messy)
- assigning event handlers by passing the function object directly
- unspeakably more consice .load and .post functions.
I used to pride myself on writing fancy AJAX interfaces for each individual application. But golly, nothing I ever did approached the simplicity and elegance of just doing $('#item').load('/url/path');.
Don't reinvent the wheel. Stand on the shoulders of giants and move on to developing something else.

Commenting is closed for this article.
Dynamic Accessor Methods Roku BrightScript Syntax Highlighting in Vim
