knockout.js - knockoutjs viewmodel not loading ajax return -
i'm new knockout , have been having trouble loading ajax json return viewmodel. i"m using knockoutjs mapping plugin.
here json return:
{"operator":[{"employeeid":394,"category":"mc","opfirstname":"fred", "opsurname":"penner","regnumber":"a12234","status":"ft","supervisorid":0,"team":"a", "teamgroup":"a1","issupervisor":"y"}]} here code:
<table> <tbody data-bind="foreach: opviewmodel.items"> <tr> <td><ul> <li data-bind="text: opfirstname"></li> </ul> </td> </tr> </tbody> </table> <script type="text/javascript"> function opviewmodel() { var self = this; $.getjson("../controllers/getallops.php", function (data) { opviewmodel.model = ko.mapping.fromjs(data); console.log(data); }); } ko.applybindings(new opviewmodel()); </script> the json created using php_encode. i've tried using "foreach: operator" still had no luck. tips appreciated. great.
thanks.
this line:
<tbody data-bind="foreach: opviewmodel.items"> should be
<tbody data-bind="foreach: model.operator"> and bit
$.getjson("../controllers/getallops.php", function (data) { opviewmodel.model = ko.mapping.fromjs(data); console.log(data); }); should be
$.getjson("../controllers/getallops.php", function (data) { self.model = ko.mapping.fromjs(data); console.log(data); }); there 4 problems,
- you dont need specify model class name when binding
- there no property
itemson viewmodel - calledmodel - the model applied
modelnot have propertyitems, propertyoperator(according received json) - you tried adding property definition of model, not instance of it.
Comments
Post a Comment