jho*_*orn 7 jquery events triggers event-handling
我正在玩javascript中实现事件池模式,类似于此处描述的内容:http://www.michaelhamrah.com/blog/2008/12/event-pooling-with-jquery-using-bind-和触发管理复杂的JavaScript /
我需要能够在触发事件时设置事件闭包的上下文,而不是在绑定时设置它们的上下文.是否有一些组合$ .trigger和$ .proxy的方法来做到这一点?
我只是将其放在这里供其他人谷歌搜索,但这并不是触发时间部分整个绑定的实际答案。
这是一种解决方案,它包装绑定函数以根据触发时提供的参数设置其上下文。如果你想在触发时传递参数,那就不好了。
EventPool = (function() {
function EventPool() {}
EventPool.Subscribe = function(event, fn) {
return $(this).bind(event, $.proxy(function(event, context) {
context = context != null ? context : this;
return $.proxy(fn, context)(event);
}, this));
};
EventPool.Publish = function(event, context) {
return $(this).trigger(event, context);
};
return EventPool;
})();
Run Code Online (Sandbox Code Playgroud)