简体   繁体   English

Javascript中所有类实例的常见回调

[英]Common callback for all instances of a class in Javascript

I've got a custom Javascript class that generates a JQuery mousedown callback. 我有一个自定义的Javascript类,它生成一个JQuery mousedown回调。 The mousedown callback is on $(document) and should really only be set for the first new instance, and not for any subsequent ones. mousedown回调在$(document)上,实际上应该只为第一个新实例设置,而不是为任何后续实例设置。 I've got something like the following: 我有类似以下内容:

function myclass(arg){
   this.arg = arg;
   $(document).mousedown(function(down_event){
       // start action
   }).mouseup(function(){
      // stop action
      })
}

I'd like those callbacks to only register once in the event that multiple myclass instances are crated, and not at all if none are created. 我希望那些回调只在多个myclass实例被装箱的情况下注册一次,如果没有创建,则根本不注册。

You should use a variable to flag if the events have already been registered and register them only if they haven't already been registered. 您应该使用变量来标记事件是否已经注册,并且只有在尚未注册的情况下才注册它们。

An example of this: 一个例子:

var registered = false; // the 'flag' variable, default to false (i.e. not yet registered)

function myclass(arg){
   this.arg = arg;

   if (!registered) { // check the 'flag' variable if events have been registered yet
      registered = true; // set the 'flag' variable as events will be registered this time

      $(document).mousedown(function(down_event){
         // start action
      }).mouseup(function(){
         // stop action
      })
   }
}

Here are a couple of possible options. 这里有几个可能的选择。

Option 1: A global var 选项1:全局变量

function myclass(arg){
   this.arg = arg;

   if (!window._myClassCreatedAlready) {
      $(document).mousedown(function(down_event){
          // start action
      }).mouseup(function(){
         // stop action
      })
   }
   window._myClassCreatedAlready = true;
}

Option 2: jQuery Data 选项2:jQuery数据

function myclass(arg){
   this.arg = arg;

   if (!$.data(document, "mousedownset")) {
      $(document).mousedown(function(down_event){
          // start action
      }).mouseup(function(){
         // stop action
      })
   }
   $.data(document, "mousedownset", true);
}

There's a jQuery function for that. 有一个jQuery函数。 Use .one() to bind a handler to the first instance of an event raised on an element (in this case document ). 使用.one()将处理程序绑定到在元素上引发的事件的第一个实例(在本例中为document )。

function myclass(arg){
    this.arg = arg;
    $(document)
        .one('mousedown.yourEvent', downHandler)
        .one('mouseup.yourEvent', upHandler);
}

function downHandler(e) {
    // start action
}

function upHandler(e) {
    // stop action

    //ensure event dead forever
    $(document).on('.yourEvent', function() { return false; });
}

Updated. 更新。 The changes (using named handlers rather than anonymous functions, putting events in a specific namespace) are to ensure that new instances of myclass don't rebind to the event if they are created after the first one has finished being unbound. 更改(使用命名处理程序而不是匿名函数,将事件放在特定命名空间中)是为了确保myclass的新实例不会重新绑定到事件(如果它们是在第一个完成未绑定之后创建的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM