从另一个数组中获取具有相同项目的项目的最佳实践是什么
好吧,我知道标题很复杂,但是这个问题也很难用一句话来表达……抱歉。
目标:我想在所有模块可用时运行回调 问题:最快的方法是什么? 示例:
问题始于函数“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”模块,然后我想知道女巫回调是否已准备好运行;
回调可能有多个依赖项 “a”模块可能被多个回调使用 这就是为什么有点复杂
Ok, I know the title is quite complicated but the question is hard to make in one line too.. sorry.
Objective: I want to run a callback when all modules are available
Problem: what is the fastest way?
Example:
the problem begins in the function "runCallbacks" that gets all the new added objects like this:
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
what I was thinking of is this:
callbacks = [[[callbackFunction],['loadedItems'],['notLoadedItems']]]
|-----------it's a single callback--------------------|
has good performance or do you have any better idea?
thanks
Another example
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
so if a callback needs "a" and "b" but not exists saves the callback for when both are ready
I add the "a" and "b" module and then I want to know witch callbacks are ready to run;
a callback might have multiple dependences
the "a" module might be used by multiple callbacks
that's why is a little bit complicated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您正在运行jquery,请查看$.when,否则还有像promise.js这样的库(此处说明:http://blogs.msdn.com/b/rbuckton/archive/2010/01/29/promises-and-futures-in-javascript.aspx)。这个想法是当依赖关系准备好并解决时,承诺会触发事件。
If you are running jquery have a look at $.when, otherwise there are libraries like promises.js (explination here: http://blogs.msdn.com/b/rbuckton/archive/2010/01/29/promises-and-futures-in-javascript.aspx). The idea is the promise fires the event when the dependacies are ready and resolved.