我认为这里有些不对劲。我想添加一个ko.computed
这样的ko.observableArray
。
在此之前我的模型清晰
//var job = @Html.Raw(Json.Encode(Model.job));
//var customer = @Html.Raw(Json.Encode(Model.customer));
//var estimateMaterials = @Html.Raw(Json.Encode(Model.estimateMaterials));
//var estimate = @Html.Raw(Json.Encode(Model.estimate));
var estimateTasks = @Html.Raw(Json.Encode(Model.Tasks));
var JobPostViewModel = function(){
var self = this;
//self.customer = ko.observable(customer);
//self.job = ko.observable(job);
//self.estimateMaterials = ko.observableArray(estimateMaterials);
//self.estimate = ko.observable(estimate);
self.estimateTasks = ko.observableArray(estimateTasks);
self.estimateTasks().estLaborSubTotal = ko.computed(function () {
return (isNaN(self.EstHr)? 0: +self.EstHr) * (isNaN(self.TaskPerHourCost)? 0: +self.TaskPerHourCost);
});
};
var model = new JobPostViewModel();
ko.applyBindings(model, document.getElementById("my_job_form"));
所以这就是我的模型绑定。my_job_form
是data-bind="with:jobs"
并且我在绑定到的表单内填充一个表estimateTasks
。标记是
<tbody data-bind="foreach: $root.estimateTasks">
<tr>
<td>
<input type="text" data-bind="value: EstHr" />
<input type="hidden" data-bind="value: TaskPerHourCost" />
</td>
<td>
<input type="text" data-bind="value: estLaborSubTotal" disabled />
</td>
</tr>
</tbody>
在绑定时,我收到错误
ReferenceError:未定义 estLaborSubTotal
我在这里做错了什么?