Tuesday, March 22, 2011

HW8 Graded and My Partial Solution

Earlier today I emailed the grades for the last homework. Most of you did not do as well as usual. Things to get complicated once we start to try to put all these topics together into a nice app. Below is my partial solution to the homework (also here, just view-source). It's in the form of a jsfiddle so you can open it on its own window and start playing around with it to make sure you understand how its doing its magic, just press the '+' on the top right. I might be upgrading it as you provide me with bug reports.

A few tips:

  1. As mentioned in class, the .live jQuery function comes in really handy when you want to register events to elements that (dis)appear dynamically. With it, you don't have to remember to re-register events on each creation (and delete on deletion to avoid memory leaks).
  2. The .each() function is also very useful.
  3. You don't need to have ids for everything. The jQuery selectors are very powerful: use them.


  1. the requirements state that the input field must have a save and cancel button, including these buttons would cause your focusout to fail, as clicking on the button would cause a focuout event and the field would disappear, you could add a timeout to pause the function before leaving focus, but in one of the many rewrites I had to do, it was less than graceful. And I had to completely remove the focus out events from my code.

  2. Yes. I did not add the save and cancel buttons because I wanted to keep the example as short as possible: short code is easier to understand.

    Getting the save and cancel buttons to work requires a couple more functions: see http://jsfiddle.net/jmvidal/zwAQF/

    I think one must do either focusout or 'save button', but not both. If the focusout saves the text then having a 'save' button is redundant (actually, the button would be useless then because before you hit it you Have to focusout of the textbox anyway).

  3. In a production application, wouldn't it be better from a user experience standpoint to have an explicit way of saving the new text(making the change)? In other words, from a user interface standpoint, just wondering if a focus out should actually save the information? If instead, it reverted back to the original state, this would prevent accidental field changes... I have seen both in play in apps I support at work, what is your take on this?

  4. Oh, and yes, there is a bug in that previous fiddle: if the user clicks on a second choice before hitting 'save' on the first one the code will get confused. Hopefully our user will never do that!

    Maybe, someone can a solution for that problem?

  5. Well, in flickr the changes a user makes on the textbox are not saved until he clicks on 'save'. That is, focusout on flickr does nothing, like in my second solution.

    I think that does help prevent accidental field changes.

    OTOH, the focusout is really nice when the user has to fill out a LOT of textboxes, just type-tab-type-tab etc.

    So, I think it depends on how many textboxes the user will be changing in a row.

  6. how would you limit the number of choices to >=2 but <=5?

  7. Probably by counting children and refusing to add/delete if at the borderline.