Search code examples
jsonknockout.jsasp.net-web-apiko.observablearray

Reading & Storing JSON data into knockoutja observableArray


I have following Json returned from an 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"}]

}

I want to save every Node into seperate knockoutjs observableArray()

here is

my code

 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()



    });
    }

I want to load data to proper observable for every node for Skills i want to store in self.Skills observable, for JobAssignements i want to store it into self.JobAssignments observableArray and voice versa. Thanks in Advance.


Solution

  • Have you tried Knockout's mapping plug-in. That would seem to do what you want automatically.