我有一个淘汰应用程序,其中Estimate是页面的主要对象。估计包含一个observableArray的WorkOrder对象(WorkOrdersDTO),但一次只显示一个WorkOrder对象进行编辑。用户可以通过从选择列表中选择来选择显示哪个WorkOrder进行编辑。我遇到的问题是我使用名为CurrentWorkOrder的对象来引用当前正在编辑的WorkOrder。设置此对象时,我遇到了一些性能问题。估计包含的WorkOrders越多,这些性能问题就越严重。当用户添加WorkOrder时,会出现最大的问题。这是该过程的代码:
self.addWorkOrder = function () {
tempWOId += 1;
var workOrder = new WorkOrder();
workOrder.WorkOrderId(tempWOId);
workOrder.WorkOrderNo(self.SetWorkOrderNo());
self.estimate().WorkOrdersDTO.push(workOrder);
self.estimate().CurrentWorkOrder($.grep(self.estimate().WorkOrdersDTO(), function (wo) { return wo == workOrder })[0]);
};
该功能的最后一行是花费最多时间的一行。我也试过使用它,但它甚至更慢。
self.estimate().CurrentWorkOrder(self.estimate().WorkOrdersDTO()[self.estimate().WorkOrdersDTO().length - 1]);
我还在我的html中删除了'with'绑定,但是没有看到任何结果。我甚至评论了整个html,其中发生了绑定,以确保性能问题不是由此引起的,并且性能没有变化。
有什么想法吗?
我回去了,用几种不同的方式计算了这行代码,结果如下: CurrentWorkOrder($。grep) - 613.000ms
CurrentWorkOrder(WorkOrdersDTO.length) - 740.000ms
CurrentWorkOrder(workOrder) - 689.000ms
没有html引用的CurrentWorkOrder($。grep) - 317.000ms
当删除绑定到CurrentWorkOrder的HTML时会有更好的性能,这让我相信这是一个问题。这是该部分的整个HTML:
<div id="work-order-details">
<div class="work-order-details" data-bind='with: estimate' style="margin-bottom:5px;">
<table style="width:100%">
<tr>
<td>
<table>
<tr>
<td style="text-align:right;">
Group
</td>
<td>
<select class="k-input" data-bind='options: WorkOrderGroupsDTO, value: CurrentWorkOrder().WorkOrderGroupId, optionsValue: "WorkOrderGroupId", optionsText: "GroupName", optionsCaption: "--Select--"'></select>
</td>
<td>
<span id="AddWorkOrderGroupButton"><span class="icon-add" data-bind='click: $root.addWorkOrderGroup'></span>
</td>
<td>
Service Date
</td>
<td>
<input style="width:120px;" id='service-date' data-bind="value: CurrentWorkOrder().ServiceDateDisplay" />
</td>
</tr>
</table>
</td>
<td style="text-align:right;">
Additional Work Orders <select id='workOrderSelect' class="k-input num-textbox" data-bind="options: WorkOrdersDTO, optionsValue: 'WorkOrderId', optionsText: 'WorkOrderNo'"></select>
</td>
</tr>
</table>
</div>
<div class="work-order-details" data-bind='with: estimate'>
<table class="table-container" style="border:solid 1px darkgray;">
<tr>
<th class="table-cell-2 estimate-wo-header" colspan="2">INSTRUCTIONS</th>
</tr>
<tr class="table-row">
<td colspan='2'>
<textarea class="textarea" name="" id="estimate-textarea" rows="4" data-bind='value: CurrentWorkOrder().Instructions' placeholder='Enter Instructions...'></textarea>
</td>
</tr>
<tr>
<th class="table-cell-2 estimate-wo-header labor-tasks">LABOR INFORMATION
<a href="#" data-bind='click: $root.addWorkOrderLaborTask'>Add</a>
</th>
<th class="table-cell-2 estimate-wo-header packing-materials">PACKING MATERIALS<a href="#" data-bind='click: $root.addWorkOrderPackingMaterial'>Add</a></th>
</tr>
<tr class="table-row">
<td class="table-cell-3 labor-tasks" style="border:solid 1px darkgray;padding-left:10px;padding-bottom:10px;">
<table class="estimate-table">
<tr class="header-row">
<td>Task</td>
<td>Qty</td>
<td>Hours</td>
<td>RT Rate</td>
<td>OT Rate</td>
<td>PT Rate</td>
<td>Cost</td>
</tr>
<tbody class='table-body' data-bind="foreach: CurrentWorkOrder().WorkOrderLaborTasksDTO">
<tr>
<td>
<select class="k-input smaller-textbox" data-bind='options: LaborTaskData, value: LaborTaskId, optionsValue: "LaborTaskId", optionsText: "Task", optionsCaption: "--Select--"'/>
</td>
<td>
<input type="number" min='0' class="k-textbox num-textbox" style="width:40px;" data-bind='value: Quantity' onkeypress="return numeric_only(event)" />
</td>
<td>
<input type="number" min='0' class="k-textbox num-textbox" data-bind='value: TotalHours' onkeypress="return numeric_only(event)" />
</td>
<td>
$0.00
</td>
<td>
$0.00
</td>
<td>
$0.00
</td>
<td>
<span data-bind='text: TotalCostDisplay'></span>
</td>
<td>
<a href='#' data-bind='click: $root.removeWorkOrderLaborTask'>X</a>
</td>
</tr>
</tbody>
</table>
</td>
<td class="table-cell-2 packing-materials" style="border:solid 1px darkgray;padding-left:10px;padding-bottom:10px;">
<table class="estimate-table">
<tr class="header-row">
<td>Material</td>
<td>Qty</td>
<td>Unit Price</td>
<td>Cost</td>
<td></td>
</tr>
<tbody class='table-body' data-bind="foreach: CurrentWorkOrder().WorkOrderPackingMaterialsDTO">
<tr>
<td><select style="width:275px;" class="k-input" data-bind="options: PackingMaterialData, value: MaterialId, optionsValue: 'MaterialId', optionsText: 'Description', optionsCaption: '--Select--'"></select></td>
<td><input type="number" min='0' class="k-textbox num-textbox" data-bind='value: Quantity' onkeypress="return numeric_only(event)" /></td>
<td><span data-bind='text: UnitPrice'></span></td>
<td><span data-bind='text: CostDisplay'></span></td>
<td><a href='#' data-bind='click: $root.removeWorkOrderPackingMaterial'>X</a></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<th class="table-cell-2 estimate-wo-header installation-products">INSTALLATION <a id='add-installation-product' href="#" data-bind="click: $root.addWorkOrderInstallationProduct">Add</a></th>
<th class="table-cell-2 estimate-wo-header unitized-items">UNITIZED PRICING <a id='add-installation-product' href="#" data-bind="click: $root.addWorkOrderUnitizedItem">Add</a></th>
</tr>
<tr class="table-row">
<td class="table-cell-2 installation-products" style="border:solid 1px darkgray;padding-left:10px;padding-bottom:10px;">
<table class="estimate-table">
<tr class="header-row">
<td>Manufacturer</td>
<td>Product Line</td>
<td>Cost</td>
<td></td>
</tr>
<tbody class='table-body' data-bind="foreach: CurrentWorkOrder().WorkOrderInstallationProductsDTO">
<tr>
<td hidden data-bind='text: WorkOrderInstallationProductId'></td>
<td>
<select class="k-input larger-textbox select-mfr" data-bind="options: ManufacturerData, value: ManufacturerId, optionsValue: 'ManufacturerId', optionsText: 'Name', optionsCaption: '--Select--'"></select>
</td>
<td>
<select data-bind="options: ProductLines, value: ProductId, optionsValue: 'ProductLineId', optionsText: 'Name', optionsCaption: '--Select--'" id="" class="k-input big-textbox"></select>
</td>
<td data-bind="text: CostDisplay"></td>
<td>
<a href='#' data-bind='click: $root.editWorkOrderInstallationProduct'>Edit</a>
<a href='#' data-bind='click: $root.removeWorkOrderInstallationProduct'>X</a>
</td>
</tr>
</tbody>
</table>
</td>
<td class="table-cell-2 unitized-items" style="border:solid 1px darkgray;padding-left:10px;padding-bottom:10px;">
<table class="estimate-table">
<tr class="header-row">
<td>Service</td>
<td>Qty</td>
<td>Unit Price</td>
<td>Cost</td>
<td></td>
</tr>
<tbody class='table-body' data-bind="foreach: CurrentWorkOrder().WorkOrderUnitizedItemsDTO">
<tr>
<td><select style="width:275px;" class="k-input" data-bind="options: UnitizedItemData, value: UnitId, optionsValue: 'UnitId', optionsText: 'Description', optionsCaption: '--Select--'"></select></td>
<td><input type="number" min='0' class="k-textbox num-textbox" data-bind='value: Quantity' style="width:40px;" onkeypress="return numeric_only(event)" /></td>
<td><span data-bind='text: UnitPrice'></span></td>
<td><span data-bind='text: CostDisplay'></span></td>
<td><a href='#' data-bind='click: $root.removeWorkOrderUnitizedItem'>X</a></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<th class="table-cell-2 estimate-wo-header storage">STORAGE & HANDLING <a id='add-storage' href="#" data-bind="click: $root.addWorkOrderStorage">Add</a></th>
<th class="table-cell-2 estimate-wo-header tech-services">TECH SERVICES<a href="#" data-bind='click: $root.addWorkOrderTechService'>Add</a></th>
</tr>
<tr class="table-row">
<td class="table-cell-2 storage" style="border:solid 1px darkgray;padding-left:10px;padding-bottom:10px;">
<table class="estimate-table">
<tr class="header-row">
<td>Inventory</td>
<td>Storage</td>
<td>Handling</td>
<td>Valuation</td>
<td>Cost</td>
</tr>
<tbody class='table-body' data-bind="foreach: CurrentWorkOrder().WorkOrderStoragesDTO">
<tr>
<td class="storage-cell"><span data-bind="text: InventoryTypeName"></span></td>
<td class="storage-cell"><span data-bind="text: EstimatedStorage"></span></td>
<td class="storage-cell"><span data-bind="text: HandlingIn"></span></td>
<td class="storage-cell"><span data-bind='text: ValuationTypeCharges'></span></td>
<td class="storage-cell"><span data-bind='text: EstimatedStorage'></span></td>
</tr>
</tbody>
</table>
</td>
<td class="table-cell-2 tech-services" style="border:solid 1px darkgray;padding-left:10px;padding-bottom:10px;">
<table class="estimate-table">
<tr class="header-row">
<td>Service</td>
<td>Qty</td>
<td>Unit Price</td>
<td>Cost</td>
<td></td>
</tr>
<tbody class='table-body' data-bind="foreach: CurrentWorkOrder().WorkOrderTechServicesDTO">
<tr>
<td><select style="width:275px;" class="k-input" data-bind="options: TechServiceData, value: UnitId, optionsValue: 'UnitId', optionsText: 'Description', optionsCaption: '--Select--'"></select></td>
<td><input type="number" min='0' class="k-textbox num-textbox" data-bind='value: Quantity' onkeypress="return numeric_only(event)" /></td>
<td><span data-bind='text: UnitPrice'></span></td>
<td><span data-bind='text: CostDisplay'></span></td>
<td><a href='#' data-bind='click: $root.removeWorkOrderTechService'>X</a></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<th class="table-cell-2 estimate-wo-header other-services">OTHER SERVICES <a id='add-other-service' href="#" data-bind='click: $root.addWorkOrderOtherService'>Add</a></th>
<th class="table-cell-2 estimate-wo-header third-party-services">THIRD PARTY SERVICES <a id='add-third-service' href="#" data-bind='click: $root.addWorkOrderThirdPartyService'>Add</a></th>
</tr>
<tr class='table-row'>
<td class="table-cell-2 other-services" style="border:solid 1px darkgray;padding-left:10px;padding-bottom:10px;">
<table class='estimate-table'>
<tr class="header-row">
<td>Description</td>
<td>Cost</td>
</tr>
<tbody class='table-body' data-bind="foreach: CurrentWorkOrder().WorkOrderOtherServicesDTO">
<tr>
<td><input style="width:360px;" class="k-textbox" type="text" data-bind="value: Description" /></td>
<td><input class="k-textbox smaller-textbox" data-bind='value: CostDisplay' onkeypress="return numeric_only(event)" /></td>
<td><a href='#' data-bind='click: $root.removeWorkOrderOtherService'>X</a></td>
</tr>
</tbody>
</table>
</td>
<td class="table-cell-2 third-party-services" style="border:solid 1px darkgray;padding-left:10px;padding-bottom:10px;">
<table class='estimate-table'>
<tr class="header-row">
<td>Description</td>
<td>Source</td>
<td>Src Cost</td>
<td>Cost</td>
</tr>
<tbody class='table-body' data-bind="foreach: CurrentWorkOrder().WorkOrderThirdPartyServicesDTO">
<tr>
<td><input class="k-textbox" type="text" data-bind="value: Description" /></td>
<td><input class="k-textbox smaller-textbox" type="text" data-bind="value: Source" /></td>
<td><input class="k-textbox mini-textbox" type="text" data-bind="value: SourceCost" /></td>
<td><input class="k-textbox mini-textbox" data-bind='value: CostDisplay' style="width:40px;" /></td>
<td><a href='#' data-bind='click: $root.removeWorkOrderThirdPartyService'>X</a></td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</div>
我当时认为不使用表可以让标记渲染更快,但我一直犹豫不决,因为这是其他开发人员更熟悉的。
答案 0 :(得分:0)
你的最后一行实际上只是一种精心设计和耗时的做法
self.estimate().CurrentWorkOrder(workOrder);
由于workOrder
是使用构造函数实例化的,因此它将是数据中唯一可以与自身进行比较的对象,因此无需搜索它。
答案 1 :(得分:0)
通过对addWorkOrder函数进行一些小的更改,我能够大大加快这个过程。
self.addWorkOrder = function () {
tempWOId += 1;
self.estimate().CurrentWorkOrder(new WorkOrder());
self.estimate().CurrentWorkOrder().WorkOrderId(tempWOId);
self.estimate().CurrentWorkOrder().WorkOrderNo(self.SetWorkOrderNo());
self.estimate().WorkOrdersDTO.push(self.estimate().CurrentWorkOrder());
};
我不是每次都将workOrder实例化为它自己的变量,而是将CurrentWorkOrder设置为新的WorkOrder并直接设置其属性。这使它大约快8倍,这应该是可以接受的。
感谢您的帮助和建议。