用于聚合外部数据的javascript设计模式

用于聚合外部数据的javascript设计模式,javascript,design-patterns,jquery,Javascript,Design Patterns,Jquery,我有点不确定该怎么问这个问题,所以我会从我现在的位置开始尽我所能。我有一种数据类型,我称之为“程序”,即从单个数据源加载一些核心信息。我已经为中心数据模型制定了加载/缓存逻辑,但是我的框架需要根据项目设置将外部加载的节点附加到数据模型中 我要寻找的是一种设计模式,它允许我在项目需要时加载这些附加的数据节点,并且在加载所有数据之前不执行程序。我可以假设,我将提前知道节点将是什么,以及在需要时从何处获取数据。我当前的设置如下所示: var programs = {}, loadCuePoin

我有点不确定该怎么问这个问题,所以我会从我现在的位置开始尽我所能。我有一种数据类型,我称之为“程序”,即从单个数据源加载一些核心信息。我已经为中心数据模型制定了加载/缓存逻辑,但是我的框架需要根据项目设置将外部加载的节点附加到数据模型中

我要寻找的是一种设计模式,它允许我在项目需要时加载这些附加的数据节点,并且在加载所有数据之前不执行程序。我可以假设,我将提前知道节点将是什么,以及在需要时从何处获取数据。我当前的设置如下所示:

var programs = {},
    loadCuePoints = function (uuid, callback) {
        //will call the callback with the data once loaded and add the program
        //to programs, keyed by the uuid
    },
    loadLinkedFiles = function (uuid, callback) {
        //will append the data to programs[uuid] and call the callback with the
        //data once loaded
    },
    loadProgram = function (uuid, callback) {
        //will append the data to programs[uuid] and call the callback with the
        //data once loaded
    },
    // hash where the key is the node on program and the value is the function
    // used to load the data, this will be based on project settings, but I'm not
    // concerned with this logic for this question
    requiredData = {
        cuePoints : loadCuePoints,
        linkedFiles : loadLinkedFiles
    },
    getProgram = function(uuid, callback) {
        if (programs[uuid]) {
            callback(programs[uuid]);
        } else {
            //assume the key in the requiredData hash is the required node on
            //Program, and that the value is the callback method, the functions
            //in this table sre already set up to load the data and return it
            //via the callback once loaded
        }
    }

我当然可以解决这个问题,所以我不需要太多的解决方案(除非你有一些工作非常好或特别优雅的解决方案),我要问的是,在一系列异步操作之后,是否有一个既定的回调排队模式。如果解释不清楚,我很乐意详细说明。

对于使用javascript的asyn编程,您应该使用promise模式。 When.js或jquery.deffered可用于解决您的问题。下面编写了使用jquery的伪代码

  function oneArrayJquery(value) {
            var deffered = jQuery.Deferred();
            deffered.resolve(value);
            return deffered;
        }

        function loadAllArrayValues(imagearray) {
            var deffered = [];
            for (var i = 0; i < imagearray.length; i++) {
                deffered.push(oneArrayJquery(imagearray[i]));
            }
            return deffered;
        }

        var arrayvalue = [1, 3, 4];

        jQuery.when(loadAllArrayValues(arrayvalue)[0], loadAllArrayValues(arrayvalue)[1]).then(function (value1,value2) {
            alert(value1);
            }
        )
函数oneArrayJquery(值){
var defered=jQuery.Deferred();
解析(值);
返回差;
}
函数LoadAllArrayValue(imagearray){
var微分=[];
对于(var i=0;i
Dojo做你想做的事,我相信。我正在为这一个滚动我自己的框架,所以我将无法使用Dojo,谢谢。我的意思是“看看Dojo,看看你是否可以重用他们的一部分理念,让你的代码更容易做你想做的事情,而不是费力地完成所有困难的部分”,因为我将在不久的将来做类似的事情。明白了,道歉,我会做的。关于承诺的进一步信息。(有一个概念和一个库,选择与您相关的)~请注意,node.js是一个处理javascript请求的服务器,javascript是javascript就是javascript,因此在页面中使用它和在服务器中使用它在承诺方面是完全相同的。完美!!!这正是我想要的。在这一款上,我们必须从头开始,但这一款看起来相当强劲/音质不错等等。