简体   繁体   English

JavaScript异步调用文字对象函数

[英]JavaScript asynchronous call to literal object function

I have this code that I use in a project which is prototype as 'data access' class where all the calculations needed and the sample data exists. 我有此代码,我在一个作为“数据访问”类原型的项目中使用了该代码,其中所有需要的计算和示例数据都存在。 Problem is that if I had these in a server 'Web Service' I could use a 'load indicator' to tell the user that I'm currently doing some work so please wait. 问题是,如果我在服务器“ Web Service”中拥有这些文件,则可以使用“负载指示器”来告诉用户我当前正在做一些工作,因此请等待。

The 'data access' code: “数据访问”代码:

(function () {
         DataAccess.db = {
                      profitList: [{ CalendarActualDateDDMMYYYY: '01/07/2009', CalendarActualDateMMDDYYYY: '07/01/2009', CalendarMonth: 7, CalendarYear: 2009, MonthName: 'July', DateInt: 200907, FiscalMonth: 1, FiscalYear: 2010, Quarter: 'Q1', Division: 'Holdings', Company: 'Holdings', RevenueYTD: 4900, RevenuePeriodic: 4900, AdjustedOperatingProfitYTD: 392, AdjustedOperatingProfitPeriodic: 392, ProfitBeforeTaxYTD: 372, ProfitBeforeTaxPeriodic: 372, OperatingMarginYTD: 8, OperatingMarginPeriodic: 8, OperatingMarginPeriodic6MonthsMovingAverage: 0 },
        { CalendarActualDateDDMMYYYY: '01/08/2009', CalendarActualDateMMDDYYYY: '08/01/2009', CalendarMonth: 8, CalendarYear: 2009, MonthName: 'August', DateInt: 200908, FiscalMonth: 2, FiscalYear: 2010, Quarter: 'Q1', Division: 'Holdings', Company: 'Holdings', RevenueYTD: 8673, RevenuePeriodic: 3773, AdjustedOperatingProfitYTD: 656, AdjustedOperatingProfitPeriodic: 264, ProfitBeforeTaxYTD: 623, ProfitBeforeTaxPeriodic: 251, OperatingMarginYTD: 7.56, OperatingMarginPeriodic: 7, OperatingMarginPeriodic6MonthsMovingAverage: 0 },
        { CalendarActualDateDDMMYYYY: '01/09/2009', CalendarActualDateMMDDYYYY: '09/01/2009', CalendarMonth: 9, CalendarYear: 2009, MonthName: 'September', DateInt: 200909, FiscalMonth: 3, FiscalYear: 2010, Quarter: 'Q1', Division: 'Holdings', Company: 'Holdings', RevenueYTD: 12687, RevenuePeriodic: 4014, AdjustedOperatingProfitYTD: 977, AdjustedOperatingProfitPeriodic: 321, ProfitBeforeTaxYTD: 928, ProfitBeforeTaxPeriodic: 305, OperatingMarginYTD: 7.7, OperatingMarginPeriodic: 8, OperatingMarginPeriodic6MonthsMovingAverage: 0 }], 
    // The list can go on big to 640 objects
                     getOperationProfitByCompany: function (company) {
                                 // various queries using SQLike ot jLinq on the profiList
                     },
                     // other same function data access workers
    })();

I'm using knockout.js, HTML5 and MVVM to bind a ViewModel to my page. 我正在使用基因敲除.js,HTML5和MVVM将ViewModel绑定到我的页面。 This is one of the pages ViewModel which will use the data access object to show some charts and custom template controls with heavy calculations in the data access methods. 这是ViewModel页面的其中之一,它将使用数据访问对象显示一些图表和自定义模板控件,这些控件中包含大量的数据访问方法。

MyPrototype.op_profit = function (params) {

    var self = this;

    var vm = criteria.viewModel;

    var filterProfitList = function (singleDate, aStartDate, aEndDate) {
        var list = [];
        if (vm.allHoldingsSelected()) {
            list = DataAccess.db.getProfitConsolidateAll(singleDate, aStartDate, aEndDate);
        } else if (vm.isDivisionSelected()) {
            list = DataAccess.db.getProfitWithCriteriaDivision(
                vm.selectedDivision(), singleDate, aStartDate, aEndDate);
        } else if (vm.isCompanySelected()) {
            list = DataAccess.db.getProfitWithCriteriaDate(
                [{ CompanyId: vm.selectedCompany() }], singleDate, aStartDate, aEndDate);
        }

        return list;
    };

    var filterChartDataSource = function () {
        var list = [];

        if (vm.allHoldingsSelected()) {
            list = DataAccess.db.getOperationProfitByAllHoldings();
        } else if (vm.isDivisionSelected()) {
            list = DataAccess.db.getOperationProfitByDivision(vm.selectedDivision());
        } else if (vm.isCompanySelected()) {
            list = DataAccess.db.getOperationProfitByCompany(vm.selectedCompany());
        }

        return list;
    };

    // ... other functions and properties ...

    var viewModel = {
        // ... functions and properties ...
        profitAnalysisList: ko.observableArray(filterProfitList(self.fromDate(), self.fromDate(), self.toDate())),
        chartOptions: {
           // .. properties objects, functions
           dataSource: filterChartDataSource(),
        },
        rangeSelectorOptions: {
           // .. properties objects, functions
           dataSource: filterChartDataSource(),
        }
// Can be used in other call back event functions too
    };

    ko.applyBindings(viewModel);

   return viewModel;

Now I think you have a full view of my structure and you understand the user experience is tragic in some circumstances due to the calculations on the data access. 现在,我认为您已经全面了解了我的结构,并且您了解由于数据访问的计算,在某些情况下用户体验是悲惨的。 When the user will select to navigate to the op_profit page, it will have some seconds (4-5 sometimes maybe more) to load the page and the show it. 当用户选择导航到op_profit页面时,将需要几秒钟(4-5有时可能更长)来加载页面并显示它。

I'm not a JavaScript expert but my mind goes to asynchronous processing, something like threading in the desktop world, or calling an AJAX operation or other async APIs. 我不是JavaScript专家,但我的想法是异步处理,例如桌面世界中的线程处理,或调用AJAX操作或其他异步API。

That gets me into Web Workers which I will study and try now, but I need to know my options and your help which will solve my problem. 这使我进入了Web Workers中,我将对其进行研究和尝试,但是我需要知道我的选择和您的帮助,这些将解决我的问题。 This is something I need to do real quick :/ 这是我真正快速要做的事情:/

Summarizing, I need to load the page immediately, show a load indicator (I already know how to do this), get the data asynchronous and then append the data to the viewModel properties so it can update the binded HTML elements with the data. 总之,我需要立即加载页面,显示一个加载指示器(我已经知道如何执行此操作),使数据异步,然后将数据附加到viewModel属性,以便它可以使用数据更新绑定的HTML元素。

I solved the problem using Web Workers. 我使用Web Workers解决了该问题。 Started from this tutorial . 从本教程开始。 It's enough for almost any situation. 几乎在任何情况下都足够。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM