1

我正在尝试使用 ko.utils.arrayMap() 循环关联 JSON 对象,但是当以这种方式完成时,该函数永远不会进入循环。谁能建议我如何获得这些属性。可能我对这种风格的 JSON 使用了错误的实用程序函数?

http://jsfiddle.net/rVPBz/

var data = [
   {
      "job_id":"AM28200",
      "inspection":{
         "5":{
            "inspection_type_id":5,
            "inspection_type_name":"hydro",
            "display_name":"Requested",
            "html_name":"Hydro",
            "itp":"Stage H1. H2, H3",
            "1":{
               "dates":[
                  "2013-10-21"
               ],
               "inspectors":[
                  " "
               ]
            },
            "2":{
               "dates":[
                  "2013-10-21"
               ],
               "inspectors":[
                  " "
               ]
            },
            "3":{
               "dates":[
                  "2013-10-21"
               ],
               "inspectors":[
                  " "
               ]
            },
            "4":{
               "dates":[
                  "2013-10-21"
               ],
               "inspectors":[
                  " "
               ]
            },
            "5":{
               "dates":[
                  "2013-10-21"
               ],
               "inspectors":[
                  " "
               ]
            }
         }
      }
   },
   {
      "job_id":"AM28212",
      "inspection":{
         "5":{
            "inspection_type_id":5,
            "inspection_type_name":"hydro",
            "display_name":"Waived",
            "html_name":"Hydro",
            "itp":"Stage H1. H2, H3",
            "1":{
               "dates":[
                  "2013-10-21"
               ],
               "inspectors":[
                  " "
               ]
            }
         }
      }
   }
]

var InspectionDiary = function() {
    var self = this;
    self.jobs = ko.observableArray([])

    function Job(data){
        var self = this;
        self.job_id = data.job_id;
        self.types = ko.observableArray([])
    }

    function Type(data){
        var self = this;
        self.type_id = ko.observable(data.inspection_type_id);
        self.name = ko.observable(data.inspection_type_name);
        self.display_name = ko.observable(data.display_name);
        self.html_name = ko.observable(data.html_name);
        self.itp = ko.observable(data.itp);
        self.inspectors = ko.observableArray([])
        self.dates = ko.observableArray([]);
    }

    var jobs = ko.utils.arrayMap(data, function(item) {
        var job = new Job(item);
        var types = ko.utils.arrayMap(item.inspection, function(item) {
            var type = new Type(item)
            type.dates = ko.utils.arrayMap(item.dates(), function(item) {
                return new Dates(item);
            })
            type.inspectors = ko.utils.arrayMap(type.inspectors(), function(item) {
                return new Inspector(item);
            })
            return type;
        })
        job.types(types);
        return job;
    });
    self.jobs(jobs);
}

 ko.applyBindings(new InspectionDiary())
4

2 回答 2

2

ko.utils.arrayMap 旨在用于数组,而不是对象。

要遍历一个对象,您可以使用以下函数:

var objectMap = function(o, handler){
    var res = {};
    for(var key in o) {
       res[key] = handler(o[key])
    }
    return res;
};

正如映射文档所说:

ko.utils.arrayMap,它为数组中的每一项执行一个函数,并将函数的结果推送到一个返回的新数组中。

见小提琴

于 2013-10-25T12:51:13.613 回答
1

问题是 json 在适当的位置不包含数组。例如,检查应该是一个数组,但它不是。由于看起来您并没有在 viewModel 中做很多额外的事情,因此您可以尝试这样做以更轻松地从 JSON 映射到 VM: http: //knockoutjs.com/documentation/plugins-mapping.html

于 2013-10-25T12:40:21.837 回答