By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,222 Members | 1,436 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,222 IT Pros & Developers. It's quick & easy.

data parameter in bind method jquery

oranoos3000
100+
P: 107
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.
Nov 5 '10 #1
Share this Question
Share on Google+
5 Replies


oranoos3000
100+
P: 107
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):

Expand|Select|Wrap|Line Numbers
  1. jQuery("#selector").bind("click", {name: "barney"}, clickHandler);
And then you use it like this:

Expand|Select|Wrap|Line Numbers
  1. function clickHandler(e) {
  2.   alert(e.data.name);
  3. }
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:

Expand|Select|Wrap|Line Numbers
  1. jQuery("#selector").click(function() { clickHandler("barney")});
  2. function clickHandler(name) {
  3.   alert(name);
  4. }
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 ?
Nov 5 '10 #2

Dormilich
Expert Mod 5K+
P: 8,639
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)
Nov 5 '10 #3

oranoos3000
100+
P: 107
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
Nov 5 '10 #4

Dormilich
Expert Mod 5K+
P: 8,639
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.
Nov 5 '10 #5

oranoos3000
100+
P: 107
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
Nov 5 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.