valHooks discussion pros/cons

valHooks discussion pros/cons

I believe there are many advantages to valHooks.  Scott Gonzalez seems to have the most objection.
Notes from the pull request:
  • Val is restructured to support extension of getters/setters

    • Additions:

      • jQuery.fn.valHook( obj )

        Override the default value getter/setter for the given elements

        @param{Object} obj An object containing a get and/or a set function

      • jQuery.fn.removeValHook()

        Remove the valHook from data and revert back to default.

      • checkValHooks( elem, type )

        Private: checks for a special valHook for the given elem. Returns the hook or undefined.

        @param{element} elem Node to check

        @param{string} type "get" or "set"

      • jQuery.valHooks -- Object

        Contains special getter for option nodes, special getter/setter for select elements,

        and special getter/setter for radio/checkbox inputs

      • radioCheckVal -- Object Modularizes getter/setter for checkbox and radio inputs

  • Arguments For:

    • You can attach hooks to certain node types, as well as val hooks to any collection of elements using jQuery.fn.valHook

    • It will allow the jQuery UI dev team to add val methods for custom inputs such as datepickers, progressbars, and sliders so that you may simply call val() on these elements.

    • Modularizes the special getting and setting for options, selects, and radio/checkbox inputs (this can be done whether or not jQuery.fn.valHooks is added)

    • Plugins that javascript-replace checkboxes, radios, selects, etc. so they can be styled can utilize this to easily retrieve correct values.

  • Arguments Against:

    • jQuery.fn.valHook uses jQuery.data to attach a valHook to given elements, which decreases the performance of val. http://jsperf.com/val-enhancements-proposal
      • If we didn't want to include the ability to attach a val hook to selected elements, and only use valHooks for nodeNames and input types, the use of jQuery.data could be removed and the performance difference would be negligible.  However, John expressed he may have a way to do valHooks without using data at all.
    • There is a concern among a couple of the core dev members that this could be arbitrary and dangerous to say that a value could be anything for any element. Most are in favor as long as it is handled gracefully and is well-documented.