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,

  1. you dont need specify model class name when binding
  2. there no property items on viewmodel - called model
  3. the model applied model not have property items, property operator (according received json)
  4. you tried adding property definition of model, not instance of it.

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -