我有这个代码,我在一个项目中使用它作为“数据访问”类的原型,其中所有需要的计算和样本数据都存在。问题是,如果我在服务器“Web 服务”中有这些,我可以使用“负载指示器”告诉用户我目前正在做一些工作,所以请稍候。
“数据访问”代码:
(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
})();
我正在使用 knockout.js、HTML5 和 MVVM 将 ViewModel 绑定到我的页面。这是 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;
现在,我认为您对我的结构有了全面的了解,并且您了解由于数据访问的计算,在某些情况下用户体验是悲惨的。当用户选择导航到 op_profit 页面时,将有几秒钟(4-5 有时可能更多)来加载页面并显示它。
我不是 JavaScript 专家,但我的想法是异步处理,比如桌面世界中的线程,或者调用 AJAX 操作或其他异步 API。
这让我进入了 Web Workers,我现在将学习和尝试,但我需要知道我的选择和你的帮助,这将解决我的问题。这是我需要快速做的事情:/
总而言之,我需要立即加载页面,显示加载指示器(我已经知道如何执行此操作),异步获取数据,然后将数据附加到 viewModel 属性,以便它可以使用数据更新绑定的 HTML 元素。