Skip to Content | Skip to Navigation


dojo/_base/connect

dojo/_base/connect contains functions for connecting to methods, events and topics. The functionality in this module is deprecated, but kept in the Dojo 1.X code stream for backwards compatiblity.

  • If you want to connect to an event, see dojo/on instead.
  • If you want to connect to a method, use Aspect Oriented Programming with after advice and see dojo/aspect::after() instead.
  • If you want to publish or subscribe to topics, see dojo/topic instead.

Usage

As with the rest of dojo/_base modules, if you are running the Dojo loader in legacy mode (async: false) this module is automatically loaded. Even if it is automatically loaded, you should require it in to have access to its features:

require(["dojo/_base/connect"], function(connect){
  // connect now has the module's features
});

Features

MethodReturnsDescription
connect(obj,event,context,method,dontFix)undefined

dojo.connect is a deprecated event handling and delegation method in Dojo. It allows one function to "listen in" on the execution of any other, triggering the second whenever the first is called. Many listeners may be attached to a function, and source functions may be either regular function calls or DOM events.

Connects listeners to actions, so that after event fires, a listener is called with the same arguments passed to the original function.

Since dojo.connect allows the source of events to be either a "regular" JavaScript function or a DOM event, it provides a uniform interface for listening to all the types of events that an application is likely to deal with though a single, unified interface. DOM programmers may want to think of it as "addEventListener for everything and anything".

When setting up a connection, the event parameter must be a string that is the name of the method/event to be listened for. If obj is null, kernel.global is assumed, meaning that connections to global methods are supported but also that you may inadvertently connect to a global by passing an incorrect object name or invalid reference.

dojo.connect generally is forgiving. If you pass the name of a function or method that does not yet exist on obj, connect will not fail, but will instead set up a stub method. Similarly, null arguments may simply be omitted such that fewer than 4 arguments may be required to set up a connection See the examples for details.

The return value is a handle that is needed to remove this connection with dojo.disconnect.

connectPublisher(topic,obj,event)undefined

Ensure that every time obj.event() is called, a message is published on the topic. Returns a handle which can be passed to dojo.disconnect() to disable subsequent automatic publication on the topic.

disconnect(handle)

Remove a link created by dojo.connect.

Removes the connection between event and the method referenced by handle.

isCopyKey(e)undefined

Checks an event for the copy key (meta on Mac, and ctrl anywhere else)

publish(topic,args)undefined

Invoke all listener method subscribed to topic.

subscribe(topic,context,method)undefined

Attach a listener to a named topic. The listener function is invoked whenever the named topic is published (see: dojo.publish). Returns a handle which is needed to unsubscribe this listener.

unsubscribe(handle)

Remove a topic listener.

See Also