JSAPI.info

dojo.connect

Dojo (1.6.1) - see full source
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
                        /*Boolean?*/ dontFix){
    // summary:
    //        `dojo.connect` is the core 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.
    //
    // description:
    //        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, `dojo.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`.
    //
    // obj:
    //        The source object for the event function.
    //        Defaults to `dojo.global` if null.
    //        If obj is a DOM node, the connection is delegated
    //        to the DOM event manager (unless dontFix is true).
    //
    // event:
    //        String name of the event function in obj.
    //        I.e. identifies a property `obj[event]`.
    //
    // context:
    //        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,
    //        dojo.global is used.
    //
    // method:
    //        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:
    //        If obj is a DOM node, set dontFix to true to prevent delegation
    //        of this connection to the DOM event manager.
    //
    // example:
    //        When obj.onchange(), do ui.update():
    //    |    dojo.connect(obj, "onchange", ui, "update");
    //    |    dojo.connect(obj, "onchange", ui, ui.update); // same
    //
    // example:
    //        Using return value for disconnect:
    //    |    var link = dojo.connect(obj, "onchange", ui, "update");
    //    |    ...
    //    |    dojo.disconnect(link);
    //
    // example:
    //        When onglobalevent executes, watcher.handler is invoked:
    //    |    dojo.connect(null, "onglobalevent", watcher, "handler");
    //
    // example:
    //        When ob.onCustomEvent executes, customEventHandler is invoked:
    //    |    dojo.connect(ob, "onCustomEvent", null, "customEventHandler");
    //    |    dojo.connect(ob, "onCustomEvent", "customEventHandler"); // same
    //
    // example:
    //        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:
    //        When globalEvent executes, globalHandler is invoked
    //        with the same scope (this):
    //    |    dojo.connect(null, "globalEvent", null, globalHandler);
    //    |    dojo.connect("globalEvent", globalHandler); // same
 
    // normalize arguments
    var a=arguments, args=[], i=0;
    // if a[0] is a String, obj was omitted
    args.push(dojo.isString(a[0]) ? null : a[i++], a[i++]);
    // if the arg-after-next is a String or Function, context was NOT omitted
    var a1 = a[i+1];
    args.push(dojo.isString(a1)||dojo.isFunction(a1) ? a[i++] : null, a[i++]);
    // absorb any additional arguments
    for(var l=a.length; i<l; i++){    args.push(a[i]); }
    // do the actual work
    return dojo._connect.apply(this, args); /*Handle*/
}