对 Knockout(和 Durandal/Breeze)非常陌生。. .
我正在使用 Knockout 的 ForEach 显示项目中每个员工的几列预测和实际小时数。接下来我想做的是计算每列的预测小时数和实际小时数的差异(hourDiff);我还想对每个员工的名字和姓氏进行格式化。数据来自服务器,我担心我可能已经把自己画到了一个角落,试图让一切正常工作到这一点。小时数实际上嵌套在每个员工对象中。
[n, n]
0: n
1: n
employee: function dependentObservable() {
__ko_proto__: function (evaluatorFunctionOrOptions, evaluatorFunctionTarget, options) {
_latestValue: n
actualHours: Object[0]
firstName: function dependentObservable() {
forecastedHours: Object[0]
lastName: function dependentObservable() {
我尝试使用 Knockout 的购物车示例,它与我的设置有点不同,我无法让它正常工作。我也尝试使用 Knockout 的 arrayMap,但没有运气;似乎没有评估来自服务器的数据(我使用了此处找到的示例:Computed values in knockout koGrid。我的代码只是为了看看我是否可以通过任何东西。):
function Item(data) {
system.log('Within Item');
this.employee = ko.observable(data.employee);
}
var mappedData = ko.observableArray(
ko.utils.arrayMap(staffingResources, function (data) {
system.log('Within mappedData');
return new Item(data);
}
)
);
这是视图模型:
define(['durandal/system', 'durandal/app', 'durandal/activator', 'plugins/router', 'jquery', 'knockout', 'services/projectdetailmanager'],
function (system, app, activator, router, $, ko, pdm) {
var taskID;
var laborCategories = ko.observableArray();
var staffingResources = ko.observableArray();
var staffingHours = ko.observableArray();
activate = function (context) {
pdm.clearManager();
taskID = context.task
system.log("taskID = " + context.task);
staffingHours([]);
staffingResources.removeAll();
staffingResources([]);
getStaffingHours(taskID);
getLaborCategories();
getStaffingResources(taskID);
}
function getStaffingHours(taskID) {
return pdm.getStaffingHours(taskID)
.then(function (data) {
staffingHours(data);
});
};
function getStaffingResources(taskID) {
return pdm.getProjectEmployeesByTask(taskID)
.then(function (data) {
staffingResources(data);
});
};
function getLaborCategories() {
return pdm.getAllLcats()
.then(function (data) {
laborCategories(data);
});
};
hourDiff = ko.computed(function () {
return 0;
});
function save() {
pdm.saveChanges();
};
return {
activate: activate,
staffingResources: staffingResources,
forecastedHours: forecastedHours,
actualHours: actualHours,
laborCategories: laborCategories,
save: save,
hourDiff: hourDiff,
addResource: addResource
};
});
这是html(现在'hourDiff'只是为了占位符的目的而进入常规函数):
<table width="100%" border="0">
<thead>
<tr>
<td style="font-weight: bold;">Name</td>
<td style="font-weight: bold;">Labor Category</td>
<td> </td>
</tr>
</thead>
<tbody data-bind='foreach: staffingResources'>
<tr>
<td style="vertical-align: top;"><span data-bind="text: employee().lastName()" />, <span data-bind=" text: employee().firstName()" /></td>
<td style="vertical-align: top; width: 20%">
<select data-bind="options: $root.laborCategories, optionsText: 'name', value: laborCategory, event: { change: $root.save }" /></td>
<!--Next ForEach Here-->
<!-- Test -->
<table border="1">
<tr>
<td>
<table border="1">
<tr>
<td style="font-weight: bold">Month</td>
</tr>
<tr>
<td style="font-weight: bold; width: 20%">Projected: </td>
</tr>
<tr>
<td style="font-weight: bold; width: 20%">Actual: </td>
</tr>
<tr>
<td style="font-weight: bold">Difference: </td>
</tr>
</table>
</td>
<!-- ko foreach: $data.employee().hours() -->
<td>
<table>
<tr>
<td>Month</td>
</tr>
<tr>
<td>
<input type="text" data-bind="value: forecastedHours, event: { change: $root.save }, valueUpdate: 'afterkeydown'" style="width: 50px;" /></td>
</tr>
<tr>
<td>
<input type="text" data-bind="value: actualHours, event: { change: $root.save }, valueUpdate: 'afterkeydown'" style="width: 50px;" /></td>
</tr>
<tr>
<td><span data-bind="text: $root.hourDiff()" /></td>
</tr>
</table>
</td>
<!-- /ko -->
</tr>
</table>
<!-- Test -->
</tr>
</tbody>
</table>
任何帮助,将不胜感激。为了便于查看,我在这里创建了一个小提琴:http: //jsfiddle.net/JfKkm/