data parameter in bind method jquery

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oranoos3000
    New Member
    • Jan 2009
    • 107

    data parameter in bind method jquery

    hi

    i read about bind jqeury method

    general syntax as follow as

    bind(eventType, data,listener)
    Establishes a function as the event handler for the specified event type on all elements in
    the matched set.
    Parameters

    eventType :(String) Specifies the name of the event type for which the handler is to be

    data: established. This event type can be namespaced with a suffix separated
    from the event name with a period character.
    (Object) Caller-supplied data that’s attached to the Event instance as a

    listener : property named data for availability to the handler functions. If omitted, the
    handler function can be specified as the second parameter.
    (Function) The function that’s to be established as the event handler.

    Returns
    The wrapped set.


    data prameter in above general syntax , how do i send this parameter, would please describe with an exapmle?

    thanks alot for your help.
  • oranoos3000
    New Member
    • Jan 2009
    • 107

    #2
    data paramteter in bind jquery method

    i read about data parameter in bind jquery method and that text is as follow as
    "If you only ever use the type-specific event helpers (.click(), .load(), .change(), etc.), you're potentially missing out on a really handy feature of jQuery: bind data. Bind data is data associated with the bound handler function, available to every invocation of it, but not in any "normal" variable scope. It's kind of like currying, except with an event attribute instead of arguments. You declare bind data like this (as the optional middle parameter of the .bind() method):

    Code:
    jQuery("#selector").bind("click", {name: "barney"}, clickHandler);
    And then you use it like this:

    Code:
    function clickHandler(e) {
      alert(e.data.name);
    }
    The 'e' variable is the standard jQuery event object passed to all event handler functions. The 'data' key within it contains the bind data created when the handler function was bound, if any. Even this simple case is potentially interesting because you're controlling the alert message at bind-time, not in the function itself, without the extra wrapper function you'd otherwise use:

    Code:
    jQuery("#selector").click(function() { clickHandler("barney")});
    function clickHandler(name) {
      alert(name);
    }
    But that's not the really useful bit. What you might not have noticed about the two examples is that in the first, the string "barney" is evaluated once (at bind time), but in the second it's evaluated every time the click event is dispatched. No big deal for a string, but for complex expressions that can create a performance issue. Since event handlers are typically executed more than once (and often a lot more than once), moving stuff out of the callback and into bind data can actually yield a noticeable difference in UI performance, as I learned this evening."

    but ,where is used bind data to eventhanlder in time binding ?
    Last edited by Dormilich; Nov 5 '10, 06:39 AM. Reason: please use [CODE] [/CODE] tags when posting code

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      but ,where is used bind data to eventhanlder in time binding ?
      you certainly have noticed that you (normally) can’t use parameters for event handler functions. thus you would need to use a closure to get those data to use (example 3). by using the bind() method you can pass those data to the event object (e) and get thesed data from the event object in your handler function (example 2) still retaining the easy standard event handler definition (example 1)

      Comment

      • oranoos3000
        New Member
        • Jan 2009
        • 107

        #4
        difference between send data while binding or clinking

        hi
        i understand your meaning but what is different between send static data while binding or while clicking an element

        thanks alot for your help

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          the key difference is memory usage and that you don’t have to create a wrapper function each time. (and as explained) it may not be a lot of memory for a string, but objects may need much more.

          Comment

          • oranoos3000
            New Member
            • Jan 2009
            • 107

            #6
            your meaning from object is data that is send in bind method or is function that send as parameter,
            while i send data as an object more memory is used?

            i'm confused , woule you please describe more.
            thank alot

            Comment

            Working...