I was doing a bunch of rjs stuff in rails, and lots of ajax goodness, and found myself constantly showing, and hiding the spinner and the update element (like a submit button, or other button)..

I’m a bit of a minimalist where possible and like to get rid of as much code as I can, and being as DRY as I can, and so I decided to make the following function:

  1. function updating_field(field_name, finished) {
  2. if (finished) {
  3. $(spinner).hide();
  4. $(update_ + field_name).show();
  5. } else {
  6. $(spinner).show();
  7. $(update_ + field_name).hide();
  8. }
  9. }

I simply call this method from the onclick method, or sumbit method of elements on the page and it tidies up my code considerably.

  1. <input type="submit" value="Save these changes" id="update_avatars2" onclick = "updating_avatars(false);" />

I can then call this code over and over again… In fact, I do, and I have many other helpers similar to this – which I’ll explore in other blog posts.