local-host 2015-07-18 13:06 采纳率: 100%
浏览 29

模块中的数据依赖

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);

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);

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

  • 写回答

1条回答 默认 最新

  • Memor.の 2015-07-18 13:13
    关注

    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.

    Call you method like following:

       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);
    

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

    *** EDITED***

    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
        };
    };
    

    And

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

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

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度