在模块数据相关性相关性、模块、数据

由网友(我的幸福无法靠岸)分享简介:我创建一个在线商店应用程序,由几个模块。第一模块是数据模块,其提供的数据应用程序的其余部分,以及一些有用的方法。这里是模块的主要要点是:I am creating an Online Store App, made up of a few modules.The first module is the Data...

我创建一个在线商店应用程序,由几个模块。 第一模块是数据模块,其提供的数据应用程序的其余部分,以及一些有用的方法。 这里是模块的主要要点是:

I am creating an Online Store App, made up of a few modules. The first module is the Data module, which provides the data for the rest of the app as well as a few useful methods. Here is the main gist of the module:

app.data = (function (pubsubService) {
//This is where the data is fetched
var items = app.UTILS.getAJAX('data.json', JSON.parse, false);
/* Items methods */
var getItems = function () {
    return items;
};
var getItemsLength = function () {
    return items.length;
};
function updateItemStock(item, amount) {
    item.stock -= Number(amount);
}
return {
    getItems: getItems,
    getItemsLength: getItemsLength;
};
})(app.pubsub);

该项目变种是通过Ajax调用,用下面的辅助函数:

The items var is called via Ajax, with the following helper function:

function getAJAX(url, callback, async) {
    async = async || true;
    var xhr = new XMLHttpRequest();
    xhr.open('get', url, async);
    xhr.onload = function () {
        callback(this.responseText);
    };
    xhr.send();
}

我试图让该调用与异步和关闭,也不管那,结果是错误的,因为后来的路线有哪些需要有关数据的一些信息的另一模块,但只有数据加载后来,导致错误。现在的问题是:我怎么能执行数据加载到项目上移动到下一个模块的初始化之前

I've tried to make this call with Async on and off, and regardless of that, the result is an error, because later down the line there is another module which needs some information about the data, but the data loads only LATER, causing the error. The question is: How can I enforce the data to be loaded to items before moving on to initialisation of the next module?

***编辑**** 这是另一种模块,这是依赖于数据的一个示例:

*** EDIT **** This is an example of another module, which is dependent on Data:

app.mainView = (function (data, pubsubService) {
var items = [];
var generateItems = function (firstItemIndex, stopIndex) {
    var dataLength = data.length;
    stopIndex = (stopIndex < dataLength) ? stopIndex : dataLength;
    items = data.slice(firstItemIndex, stopIndex);
    pubsubService.publish('itemsGenerated');
};
var getItems = function () {
    return items;
};

return {
    generateItems: generateItems,
    getItems: getItems
};
})(app.data.getItems(), app.pubsub);

这是否模块确实需要在Ajax回调中定义的这个工作?我不是这个解决方案的粉丝

Does this module really need to be defined inside the AJAX callback for this to work? I am not a fan of this solution

推荐答案

这要需要从阿贾克斯传来的数据被写入回调方法的任何操作。因此,只要数据是可用的code将被调用。

Any operation that wants the data coming from Ajax needs to be written in Callback method. So whenever data is available your code will be invoked.

叫你像下面的方法:

   getAJAX(url, function(data){
// write any code that want data from Ajax.
}, true);

另一种方法:

Alternative:

function doSomeThing(data)
{
// do something
}

getAJAX(url, function(data){
    doSomeThing(data);
    }, true);

Or

getAJAX(url,doSomeThing, true);

不要让电话具有异步假的。它会导致你页面的UI响应缓慢。

Do not make call having Async false. it will cause you page UI response sluggish .

*** EDITED ***

*** EDITED***

我已经用你的code简化它。

I have used your code to simplify it.

// Modified it in a method that can be put anywhere you want
app.methodName = function(data, pubsubService) {
    var items = [];
    var generateItems = function(firstItemIndex, stopIndex) {
        var dataLength = data.length;
        stopIndex = (stopIndex < dataLength) ? stopIndex : dataLength;
        items = data.slice(firstItemIndex, stopIndex);
        pubsubService.publish('itemsGenerated');
    };
    var getItems = function() {
        return items;
    };

    return {
        generateItems : generateItems,
        getItems : getItems
    };
};

// call that method in a callback like this. 
app.mainView  = app.methodName(app.data.getItems(), app.pubsub);

这是如何在回调中编写一行code,以让您的工作,这将也消除复杂性。

This is how you can write one line of code in callback to get your work done that will remove complexity also.

阅读全文

相关推荐

最新文章