Forgive my ignorance as I am not as familiar with jquery. Is there an equivalent to dojo.connect() ?
I found this solution : /
But there isn't disconnect feature !
Do you know other solution in jquery ? There are jquery.connect but this plugin not work in my tests.
Thanks for your help,
Stephane
Forgive my ignorance as I am not as familiar with jquery. Is there an equivalent to dojo.connect() ?
I found this solution : http://think-robot./2009/06/hitch-object-oriented-event-handlers-with-jquery/
But there isn't disconnect feature !
Do you know other solution in jquery ? There are jquery.connect but this plugin not work in my tests.
Thanks for your help,
Stephane
- Note that dojo.connect is being replaced by the dojo/on function: dojotoolkit/reference-guide/1.8/dojo/on.html – Steve Wranovsky Commented Oct 12, 2012 at 4:03
4 Answers
Reset to default 3The closest equivalent jQuery has is .bind()
, for example:
$("#element").bind('eventName', function(e) {
//stuff
});
And .unbind()
to remove the handler, like this:
$("#element").unbind('eventName');
There are also shortcuts for .bind()
, so for example click
can be done 2 ways:
$("#element").bind('click', function() { alert('clicked!'); });
//or...
$("#element").click(function() { alert('clicked!'); });
There is also .live()
(.die()
to unbind) and .delegate()
(.undelegate()
to unbind) for event handlers based on bubbling rather than being directly attached, e.g. for dynamically created elements.
The examples above were anonymous functions, but you can provide a function directly just like dojo (or any javascript really), like this:
$("#element").click(myNamedFunction);
What do you mean by an equivalent to dojo.connect() ?
If you are willing to create your own custom event handlers, look at bind(), trigger() and proxy() in the Events.
The proxy() function will alow your to redefine what this points to in your callback function.
jQuery connect is equivalent to dojo.connect.
There isn't as such but you can acheive the same thing as follows:
$('#someid').click(function() { SomeFunc($(this)) });
That will wire somefunc, to the click event of control with id "someid". When the control is clicked the function will be called and the parameter will be a jquery object that refers to control that was clicked.
Update I have just done a bit more research and i think .delegate() is closer to what you want.