W3cubDocs

/Dojo

dojo/_base/connect

Summary

This module defines the dojo.connect API. This modules also provides keyboard event handling helpers. This module exports an extension event for emulating Firefox's keypress handling. However, this extension event exists primarily for backwards compatibility and is not recommended. WebKit and IE uses an alternate keypress handling (only firing for printable characters, to distinguish from keydown events), and most consider the WebKit/IE behavior more desirable.

See the dojo/_base/connect reference documentation for more information.

Methods

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

Defined by dojo/_base/connect

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.

Parameter Type Description
obj Object
Optional

The source object for the event function. Defaults to kernel.global if null. If obj is a DOM node, the connection is delegated to the DOM event manager (unless dontFix is true).

event String

String name of the event function in obj. I.e. identifies a property obj[event].

context Object | null

The object that method will receive as "this".

If context is null and method is a function, then method inherits the context of event.

If method is a string then context must be the source object object for method (context[method]). If context is null, kernel.global is used.

method String | Function

A function reference, or name of a function in context. The function identified by method fires after event does. method receives the same arguments as the event. See context argument comments for information on method's scope.

dontFix Boolean
Optional

If obj is a DOM node, set dontFix to true to prevent delegation of this connection to the DOM event manager.

Returns: undefined

Examples

Example 1

When obj.onchange(), do ui.update():

dojo.connect(obj, "onchange", ui, "update");
dojo.connect(obj, "onchange", ui, ui.update); // same

Example 2

Using return value for disconnect:

var link = dojo.connect(obj, "onchange", ui, "update");
...
dojo.disconnect(link);

Example 3

When onglobalevent executes, watcher.handler is invoked:

dojo.connect(null, "onglobalevent", watcher, "handler");

Example 4

When ob.onCustomEvent executes, customEventHandler is invoked:

dojo.connect(ob, "onCustomEvent", null, "customEventHandler");
dojo.connect(ob, "onCustomEvent", "customEventHandler"); // same

Example 5

When ob.onCustomEvent executes, customEventHandler is invoked with the same scope (this):

dojo.connect(ob, "onCustomEvent", null, customEventHandler);
dojo.connect(ob, "onCustomEvent", customEventHandler); // same

Example 6

When globalEvent executes, globalHandler is invoked with the same scope (this):

dojo.connect(null, "globalEvent", null, globalHandler);
dojo.connect("globalEvent", globalHandler); // same

connectPublisher(topic,obj,event)

Defined by dojo/_base/connect

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.

Parameter Type Description
topic String

The name of the topic to publish.

obj Object
Optional

The source object for the event function. Defaults to kernel.global if null.

event String

The name of the event function in obj. I.e. identifies a property obj[event].

Returns: undefined

Examples

Example 1

dojo.connectPublisher("/ajax/start", dojo, "xhrGet");

disconnect(handle)

Defined by dojo/_base/connect

Remove a link created by dojo.connect.

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

Parameter Type Description
handle Handle

the return value of the dojo.connect call that created the connection.

isCopyKey(e)

Defined by dojo/_base/connect

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

Parameter Type Description
e Event

Event object to examine

Returns: undefined

publish(topic,args)

Defined by dojo/_base/connect

Invoke all listener method subscribed to topic.

Parameter Type Description
topic String

The name of the topic to publish.

args Array
Optional

An array of arguments. The arguments will be applied to each topic subscriber (as first class parameters, via apply).

Returns: undefined

Examples

Example 1

dojo.subscribe("alerts", null, function(caption, message){ alert(caption + "\n" + message); };
dojo.publish("alerts", [ "read this", "hello world" ]);

subscribe(topic,context,method)

Defined by dojo/_base/connect

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.

Parameter Type Description
topic String

The topic to which to subscribe.

context Object
Optional

Scope in which method will be invoked, or null for default scope.

method String | Function

The name of a function in context, or a function reference. This is the function that is invoked when topic is published.

Returns: undefined

Examples

Example 1

dojo.subscribe("alerts", null, function(caption, message){ alert(caption + "\n" + message); });
dojo.publish("alerts", [ "read this", "hello world" ]);

unsubscribe(handle)

Defined by dojo/_base/connect

Remove a topic listener.

Parameter Type Description
handle Handle

The handle returned from a call to subscribe.

Examples

Example 1

var alerter = dojo.subscribe("alerts", null, function(caption, message){ alert(caption + "\n" + message); };
...
dojo.unsubscribe(alerter);

© 2005–2017 JS Foundation
Licensed under the AFL 2.1 and BSD 3-Clause licenses.
http://dojotoolkit.org/api/1.10/dojo/_base/connect.html