阅读与阅读将JSON数据存储到knockoutja observableArray中

时间:2014-09-04 17:05:12

标签: json knockout.js asp.net-web-api ko.observablearray

我跟随Json从web api返回

  {"PayHistories":
[{"Id":3,"RateChangeDate":"2014-09-03T06:00:00","Rate":10.00,"PayFrequency":7,"JobTitle":"Production Manager","Gendre":"M"}],

"JobCondidates":
[{"Id":3,"Resume":null,"JobTitle":"Production Manager","Gendre":"M"}],

"JobAssignments":
[{"Id":2,"DateStarted":"2014-09-03T05:00:00","DateFinished":"2014-09-03T05:00:00","DepartmentName":"Sales And Distributations","JobTitle":"Production Manager","SupervisorName":"Production Manager"}],

"Skills":
[{"Id":3,"SkillName":"Designer","DateSkillAcquired":"2014-09-03T06:00:00","SkillLevelCode":"103"},{"Id":4,"SkillName":"Agile Developer","DateSkillAcquired":"2014-09-03T06:00:00","SkillLevelCode":"104"}]

}

我想将每个节点保存为单独的knockoutjs observableArray()

这是

我的代码

 self.skills=ko.observableArray();
   self.PayHistories=ko.observableArray();
   self.JobCondidates=ko.observableArray();
   self.JobAssignments=ko.observableArray();

 var employeeUri='/api/employees/';

      function ajaxHelper(uri, method, data) {
    //Clear error message.
    self.error('');
    return $.ajax({
        type: method,
        url: uri,
        dataType: 'json',
        contentType: 'application/json',
        data: data ? JSON.stringify(data) : null
    })
      .fail(function (jqXHR, textStatus, errorThrown) {

          self.error(errorThrown);


      });


}


function getAllEmployees() {
    ajaxHelper(employeesUri, 'GET').done(function (data) {



          // data is successfully loading into this data variable in this done function.
          //But i want to store data seperatly into specific observableArray() say i want to            store Skills int   self.Skills observableArray()



    });
    }

我想将数据加载到我希望存储在self.Skills observable中的Skills的每个节点的正确observable,对于JobAssignements,我想将它存储到self.JobAssignments observableArray中,反之亦然。在此先感谢。

1 个答案:

答案 0 :(得分:2)

你有没有尝试过Knockout的mapping plug-in。这似乎会自动完成你想做的事。