Unable to parse bindings in Knockout.js -
facing common "binding issue". below viewmodel
function detaillistviewmodel() { this.details = new details(); this.productdetails = ko.observablearray([]); this.show = function (item) { $.getjson("products.json", {}, function (data) { this.productdetails.push(this.details.init(data)); }); }; } function details() { this.author = ko.observable(); this.text = ko.observable(); this.init = function (temp) { return { author: temp.author, text: temp.text }; }; } var tasklistviewmodel = { tasks: ko.observablearray([]), addtask: function () { self.tasks.push(new task({ bomid: this.bomid() }, { createdby: this.createdby() })); }, showproductinfo: function (item) { detaillistviewmodel.show(item); } }; $(function () { $.getjson("tasks.json", function (alldata) { tasklistviewmodel.tasks.push(tokoobserable($.parsejson(alldata.getdataresult).result.document)); ko.applybindings(tasklistviewmodel); var detaillistviewmodel = new detaillistviewmodel(); ko.applybindings(detaillistviewmodel); }); function tokoobserable(blog) { return { bomid: ko.observable(blog[0].bomid), createdby: ko.observable(blog[0].createdby) }; } });
i getting error
uncaught error: unable parse bindings. message: referenceerror: author not defined; bindings value: text:author
my html :
<h3 data-bind="text:author">/h3>
i pretty sure if change details implementation should work expect it. not sure why details list contains single details instance , create observable array of details reusing object. not seem right design point of view. init not return observable values. try changing along these lines:
function details(author, text) { this.author = ko.observable(author); this.text = ko.observable(text); }
then when populate details list do:
function detaillistviewmodel() { this.productdetails = ko.observablearray([]); this.show = function (item) { $.getjson("products.json", {}, function (data) { this.productdetails.push(new details(data.author, data.text)); }); }; }
Comments
Post a Comment