【问题标题】:Reading & Storing JSON data into knockoutja observableArray读取 JSON 数据并将其存储到 knockoutjs observableArray
【发布时间】:2014-10-29 13:00:20
【问题描述】:

我已经关注从 web api 返回的 Json

  {"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 中的技能的每个节点加载数据到适当的 observable,对于 JobAssignements 我想将它存储到 self.JobAssignments observableArray 中,反之亦然。提前致谢。

【问题讨论】:

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


    【解决方案1】:

    您是否尝试过 Knockout 的 mapping plug-in。这似乎会自动执行您想要的操作。

    【讨论】:

    • 好的,我会研究它,看看它是否能解决我的问题。谢谢您的回复
    猜你喜欢
    • 1970-01-01
    • 2012-04-10
    • 2018-03-06
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多