2012-01-11 55 views
0

的JQuery可以处理事件实现Observer设计模式,但它只能处理一个事件一个回调函数你知道任何JavaScript API与许多前提处理?像人工智能

我想知道,如果它能够处理许多事件:当所有这些事件被触发,或当所有布尔前提成了真,则调用一个函数

这将是更好的开发动态应用

你知道,如果已经存在类似的东西?如果不是:你认为如果我发展会好吗?

编辑: 我愿做这样的事情:

when((a==b && c!=0), function(){ 
//do something when a==b and c!=0 
alert("a==b && c!=0"); 
}); 

编辑2:

,我发现了一个伟大的API,允许监听变量的变化。你只需要做这样的事情:

obj.watch(function(){ 
alert("obj changes"); 
}); 

http://watch.k6.com.br/

+0

我更新了答案,也许这会帮助更多? – Aidiakapi 2012-01-12 18:57:03

回答

0

您可以轻松地手动执行此操作:

var flag1 = false; 
var flag2 = false; 
var flag3 = false; 
var checkAllFlags = function() { 
    if (!flag1 || !flag2 || !flag3) return; 
    checkAllFlags = function() { }; 
    allFlagsSet(); 
} 
var allFlagsSet = function() { 
    // Execute here when all flags are set 
} 

// When you set a flag do it like this: 
flag2 = true; 
checkAllFlags(); 

或者你可以使用这个类:

window.flagger = function (count, callback, once) { 
    var curr = this; 
    curr.callback = callback; 
    if (once) 
     curr.called = false; 
    curr.flags = []; 
    curr.flagsLeft = count; 
    for (var i = 0; i < count; i++) 
     curr.flags[i] = false; 

    this.flag = function (index) { 
     if (!curr.flags[index]) { 
      curr.flags[index] = true; 
      if (--curr.flagsLeft <= 0 && (!once || !curr.called)) { 
       if (once) curr.called = true; 
       curr.callback(); 
      } 
     } 
    }; 
    this.unflag = function (index) { 
     if (curr.flags[index]) { 
      curr.flags[index] = false; 
      curr.flagsLeft++; 
     } 
    }; 
    this.reset = function (force) { 
     for (var i = 0; i < count; i++) 
      curr.flags[i] = false; 
     curr.flagsLeft = count; 
     if (once && force) 
      curr.called = false; 
    }; 
    this.isFlagged = function (index) { 
     return curr.flags[index]; 
    }; 
} 

而且像这样使用它:

var myFlagger = new flagger(
    /* The amount of flags that need to be set: */8, 
    /* The function that needs to be called when the flags are all set: */ 
    function() { alert('Callback function called'); }, 
    /* If the callback function should be called again when a flag is unflagged and reflagged. 
     Optional. Default: false */false); 

// You can now use these functions: 
myFlagger.flag(0); // Use this to set a flag, index ranges from 0 to count - 1. 
myFlagger.unflag(0); // Unflags an index. 
myFlagger.reset(); // Resets all flags to false. 
myFlagger.reset(true); // Using this forces the reset, returning to full start 
         // state, causes callback function to be called again 
         // even if 'once' is specified. 
alert('0 is flagged: ' + myFlagger.isFlagged(1)); // Returns if flag is flagged. 

我希望这有点帮助你。

+0

但随后当变量值发生变化时,我将不得不验证所有变量。 我想知道更多的东西像一个观察员为每个变量的前提下,当主题值变化,然后验证其他房屋如果全部是真的,然后调用回调函数 – melanke 2012-01-11 23:58:25

+0

我不知道jQuery中的这种功能,对不起:/ – Aidiakapi 2012-01-12 00:00:59

+0

@ user858257逻辑也可以使用二进制标志和位掩码来执行,但我不确定它会更容易。 https://developer.mozilla.org/zh/JavaScript/Reference/Operators/Bitwise_Operators#Example:_Flags_and_bitmasks – qw3n 2012-01-12 01:03:31

0

听起来像Promise s给我。大多数以这种概念开发的库(包括jQuery.deferred在内的JavaScript都有很多)可以简单地为其他人完成的事件构建一个新的Promise。

+0

我正在阅读$ .deffered,它确实有帮助,有些工作可能是我正在寻找的,谢谢 – melanke 2012-01-12 00:14:24

+0

我认为jQuery的承诺类不是最强大的,但如果你已经使用jQuery,它可能是dojo或[Q](https://github.com/kriskowal/q) – Bergi 2012-01-12 00:18:21

+0

的替代方案,推迟jquery的类是不是很酷!我没有找到一种方法来使用$。当我的应用程序逻辑的某些事情发生时,只是当jquery api的某些事情发生像$ ajax。 – melanke 2012-01-12 00:57:37