views:

175

answers:

3

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.com/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

A: 

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.

Ben Robinson
+1  A: 

The 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);
Nick Craver
A: 

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.

mexique1
> The proxy() function will alow your to redefine what this points to in your callback function.Great !
harobed
In dojotoolkit, I can do :dojo.connect(obj, "on_custom_event", obj2, obj2.action);obj can been DOM node or simple javascript object.In jQuery, bind work only on DOM node element.
harobed