2011-08-24 67 views
0

好吧,我知道标题是相当复杂的,但问题很难在一行中..太对不起。什么是最好的做法,以获得具有相同的项目从另一个阵列的项目

目的:我想在所有模块都可用时运行回调 问题:什么是最快的方法? 例子:

在功能问题开始“runCallbacks”是获取所有新添加的对象是这样的:

runCallbacks(newItems/*as array ['a','b','c'];*/){ 
    // now I would need to understand the dependencies of the callbacks 
    // each callback might depend on one or more objects 
    // iNeed(['a','b'], toRunThis); 
    //   |   |-callback to run when those are ready 
    //   |-are the dependencies 

我在想什么的是这样的:

callbacks = [[[callbackFunction],['loadedItems'],['notLoadedItems']]] 
       |-----------it's a single callback--------------------| 

具有良好性能还是你有更好的主意? 感谢

又如

this.use(['a', 'b'], function(){/* do something with 'a' and 'b' only when are ready */}) 
use: function(paths, callback, target/*not used in this case*/){ 
    // "a", "b", 'c' module is available 
    // "d", 'e' module is not available 
    this.callbacks.push([[target], ['a', 'b', 'c'], ['d', 'e']]); 
    //        |  |-not loaded 
    //        |-loaded 
} 

// then an object might be added 
this.add({...}) 

// then will check if this new object may make some callbacks to run 
function(newPaths/* ['d'] */){ 
    // loop all callbacks items 
    // remove items from [not loaded array] and put to [loaded array] if 
    // exists in newPaths 
    // in this case the callback already has: a,b,c; but misses: d,e; 
    // now it will add "d" to the loaded array 
    // and now only miss the "e" path 

所以如果回调需要“A”和“B”,但不存在节省了当两个准备回调 我添加了“A”和“B”模块然后我想知道女巫的回调已经准备就绪;

回调可能有多个依赖性 “一”模块可能由多个回调 这就是为什么是有点复杂

+0

您谈论的模块是'a','b'和'c'元素吗? – Jad

+0

你需要经常相交吗?或者是什么问题?你需要澄清。 – jishi

+0

@Jad:是的,这些是模块 –

回答

1

如果你正在运行的jQuery看看$。当使用,否则有像promises.js这样的库(在这里探索:http://blogs.msdn.com/b/rbuckton/archive/2010/01/29/promises-and-futures-in-javascript.aspx)。这个想法是,当有关方面已经准备好并解决时,这个承诺就会引发事件。

+0

这里的想法是作为一个框架的核心,是快速,简单和低层次的。我无法实现事件系统,因为在这种情况下不需要。在这种情况下,硬编码是更好的选择 –

相关问题