如何在JavaScript中实现简单的优先级侦听器模式 [英] How to implement a simple prioritized listener pattern in JavaScript

查看:76
本文介绍了如何在JavaScript中实现简单的优先级侦听器模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有此功能的事件/侦听器管理器:

I have an event/listener manager that has this function:

  var addListener = function(event, listener) {
    myListeners[event].push(listener); //assume this code works
  }

但现在我需要更改它以便它看起来像这样:

But now I need to change it so that it looks like this:

  var addListener = function(event, listener, fireFirst) {
    if(fireFirst) {
      myListenersToFireFirst[event].push(listener);
    } else {
      myListenersToFireSecond[event].push(listener);
    }
  }

这是当 fireEvent 函数被调用,它将首先触发 myListenersToFireFirst 数组中的侦听器,然后触发第二个数组中的侦听器。

This is so that when the fireEvent function is called, it will fire the listeners in the myListenersToFireFirst array first, then the listeners in the second array.

所以它看起来像这样:

  var fireEvent = function(event) {
    var firstListeners = myListenersToFireFirst[event];
    //for each listener in firstListeners, call `apply` on it

    var secondListeners = myListenersToFireSecond[event];
    //for each listener in secondListeners, call `apply` on it
  }



<这是在JavaScript中实现这一目标的最佳方法吗?有没有更优雅的方式来实现这个侦听器事件触发的优先级列表?

Is this the best way to accomplish this in JavaScript? Is there a more elegant way of achieving this priority list of listener-event firing?

推荐答案

也许它比我的更好。但这是一种特定的方式,我的意思是你必须在块中插入新的处理程序。这是一个更通用的工具,但它似乎适用于你的情况。

maybe its a better way then mine.. but that is a specific way, i mean you have to insert new handlers in the block. This is a more generic tool, however it seems to have application on your case.

我建议:

//makes a closured function that calls this function after the other
Function.prototype.prefix=function(o) {
    var that=this;
    return function(){
        that.apply(this,arguments);
        return o.apply(this,arguments);
    };
}
//prefix=reversed sufix
Function.prototype.sufix=function(o) {return o.prefix(this);}

使用此代码,您可以相互追加/预置函数以形成一种链,它可用于添加另一个侦听器或跟踪函数用法,或者甚至以最小的影响调整代码。

with this code you can append/prepend functions to each other to form a kind of chain, its usefull to add another listener or to track a functions usage, or even do adapt code with minimal impact.

一些用法

function audit() {console.log(arguments.callee.caller,arguments.callee.name,arguments);}
function a() {alert(arguments);}
a=audit.prefix(a);


//a user function
function f() {alert(arguments);}
f("test");//just do the alert as defined
 f=audit.prefix(a);
f("test");//now arguments will be on console too

//a builtin function
//this audit example only write to the console the arguments being passed to it
function audit() {console.log(arguments.callee,arguments);}
//auditing alert function (not too usefull, but shows it works even for
alert=audit.prefix(alert);//alert is now prefixed with audit

//an event handler
document.body.onclick=function() {alert("clicked");};
document.body.onclick=audit.prefix(document.body.onclick);

这篇关于如何在JavaScript中实现简单的优先级侦听器模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆