dojo.connect:功能强大的对象和事件监听器

One of the pieces of the Dojo Toolkit I've been really impressed with is dojo.connect. This connect method not only listens to DOM events but also allows you to listen to when a regular function is executed. Lets examine what dojo.connect is and the different methods by which you can use dojo.connect within any Dojo application.

我印象深刻的Dojo工具包之一是dojo.connect 。 此connect方法不仅侦听DOM事件,而且还允许您侦听执行常规函数时的情况。 让我们检查一下dojo.connect是什么,以及可以在任何Dojo应用程序中使用dojo.connect的不同方法。

dojo.connect的签名 (dojo.connect's Signature)


dojo.connect = function(
	obj,		//Object|null
	event,		//String
	context,	//Object|null
	method,		//String|Function
	dontFix		//Boolean
) //.....


A more detailed explanation of each argument (as taken from the Dojo documentation):

每个参数的更详细说明(摘自Dojo文档)

  • 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).

    obj:事件函数的源对象。 如果为null,则默认为dojo.global。 如果obj是DOM节点,则将连接委派给DOM事件管理器(除非dontFix为true)。

  • event: Name of the event function in obj. I.e. identifies a property obj[event].

    event: obj中事件函数的名称。 即标识属性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.

    context:该方法将以“ this”形式接收的对象。 如果context为null并且method是函数,则method继承事件的上下文。 如果method是字符串,则context必须是方法的源对象对象(context [method])。 如果context为null,则使用dojo.global。

  • 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.

    dontFix :如果obj是DOM节点,请将dontFix设置为true,以防止将此连接委托给DOM事件管理器。

dojo.connect returns a handle that you will need to remove the connection later. Also note that any arguments passed to the object (function) will be received by the listener! Holy hell that's useful!

dojo.connect返回一个句柄,稍后您将需要删除该句柄。 还要注意, 传递给对象(函数)的所有参数都将由侦听器接收! 天哪,这很有用!

用法示例:DOM节点事件处理 (Example Usages: DOM Node Event Handling)


var eventHandle = dojo.connect(dojo.byId('myElement'), 'onclick', null, function() { //null = dojo.global
	alert('you clicked myElement');
});

//...or:

var eventHandle = dojo.connect(dojo.byId('myElement'), 'onclick', function() { //context isn't required
	alert('you clicked myElement');
});



When myElement is clicked, my messaged is alerted.

单击myElement时 ,将通知我的消息。

用法示例:对象处理 (Example Usages: Object Handling)


var someFunction = function() {
	alert('run!');
};
var val = dojo.connect(null, 'someFunction', null, function() { //null = dojo.global
	alert('you ran someFunction!()');
});


When someFunction is called, my listener function alerts the message. How cool is that?!

调用someFunction时 ,我的侦听器函数会提醒该消息。 多么酷啊?!

用法示例:NodeList事件处理 (Example Usages: NodeList Event Handling)


dojo.query('.myElements').connect('onclick', function() { //"this" becomes the "current" element
	alert('you clicked this link');
});

Event handling even works on dojo.connect -- you don't need to cycle through each element in the collection to add the event listeners individually.

事件处理甚至可以在dojo.connect上运行 -您不需要循环遍历集合中的每个元素即可单独添加事件侦听器。

用法示例:删除事件 (Example Usage: Removing an Event)


var eventHandle = dojo.connect(dojo.byId('myElement'), 'onclick', function() { //context isn't required
	alert('you clicked myElement');
});

//....
//something happens; we want to remove the event listener
//....

dojo.disconnect(eventHandle);

We pass the handle provided by dojo.connect to disconnect the listener.

我们传递由dojo.connect提供的句柄以断开侦听器的连接。

That's just a quick taste of what you can do with dojo.connect. Be sure to check out Dojo and experiment with the different ways you can use dojo.connect. Since dojo.connect is one of the core methods of the library, dojo.connect is used everywhere. dojo.connect also plays an important role within Dijit, Dojo's UI branch.

这只是您可以使用dojo.connect进行的快速体验。 确保检查Dojo并尝试使用dojo.connect的不同方法。 由于dojo.connect是库的核心方法之一,因此dojo.connect随处可见。 dojo.connect在Dojo的UI分支Dijit中也起着重要的作用。

翻译自: https://davidwalsh.name/dojo-connect

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值