2013-10-25 70 views
1

我想通過使用ko.utils.arrayMap()來循環關聯的JSON對象,但函數永遠不會以這種方式進入循環。任何人都可以建議我如何獲得這些屬性。可能我對這種JSON使用了錯誤的效用函數?ko.utils.arrayMap與關聯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()) 

回答

3

ko.utils.arrayMap被設計用於數組,而不是一個對象。

要通過一個對象可以用下面的函數的循環:

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

作爲mapping doc說:

ko.utils.arrayMap,其在陣列執行的函數的每個項目並將函數的結果推送到返回的新數組中。

See fiddle

1

的問題是JSON不包含在適當的地方陣列。例如,檢查應該是一個數組,而不是。由於看起來你並沒有在viewModels中做很多額外的工作,你可以嘗試一下從JSON到VM的映射:http://knockoutjs.com/documentation/plugins-mapping.html

+0

感謝您的幫助!現在有道理!你知道我有什麼辦法可以迭代對象嗎? – Wellso

+0

試試這個http://api.jquery.com/jQuery.each/ – pax162