knockout.js - Why does adding a computed to an observableArray fail? -
i think wrong here. add ko.computed
ko.observablearray
this.
before model clarity
//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"));
so model bind is. my_job_form
data-bind="with:jobs"
, populating table inside form bound estimatetasks
. markup is
<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>
on bind, error
referenceerror: estlaborsubtotal not defined
what doing wrong here?
you adding computed array. array result of evaluation of estimatetasks observablearray();
if understand trying do.
you better way. add computed named estlaborsubtotal item.
var jobpostviewmodel = function(){ var self = this; ko.utils.arrayforeach(estimatetasks, function(et) { et.estlaborsubtotal = ko.computed({ read: function(){ var esthr = this.esthr(); var taskperhourcost = this.taskperhourcost(); if(esthr === null) return 0; return esthr * taskperhourcost; }, owner : et // specify "this" during evaluation }); }); self.estimatetasks = ko.observablearray(estimatetasks); };
i hope helps.
Comments
Post a Comment