【发布时间】:2012-10-14 20:47:16
【问题描述】:
鉴于此 json 结构:
{
"categoryID" : 1,
"categoryName" : "Stupid Questions",
"questions" : [{
"question" : [{
"questionOptions" : [{
"questionOptionID" : 1,
"optionText" : "It's top secret."
}, {
"questionOptionID" : 2,
"optionText" : "Because I am big and your small. I am right and your wrong."
}, {
"questionOptionID" : 3,
"optionText" : "I will gladly pay you Tuesday for a hamburger today."
},
],
"questionType" : "checkbox",
"questionText" : "Why can't we use more abstract table and column names?",
"summary" : "Question of the year"
}
]
}
]
}
我想将 questions 和 questionOptions 都映射到 template 和 templateOptions:
{
"categoryID" : 1,
"categoryName" : "Stupid Questions",
"templates" : [{
"template" : [{
"templateOptions" : [{
"templateOptionID" : 1,
"optionText" : "It is top secret."
}, {
"QuestionOptionID" : 2,
"OptionText" : "Because we are lazy."
}, {
"QuestionOptionID" : 3,
"OptionText" : "I will gladly pay you Tuesday for a hamburger today."
},
],
"QuestionType" : "checkbox",
"QuestionText" : "Why can't we use more abstract table and column names?",
"Summary" : "Question of the year"
}
]
}
]
}
这是我的淘汰映射对象的开始:
var templateMapping = {
'templates': {
templates: function(data) {
return ko.utils.unwrapObservable(data.questions);
}
}
//how do I map observable array of question options to observable array of template options here?
};
这个映射的关键是子对象有不同的结构(不像这个问题 - https://stackoverflow.com/a/7535397/466321)。似乎我发现的所有映射示例都没有涵盖如何完成此操作,而且我尝试了自己的几个理论都没有成功。
【问题讨论】:
-
映射器不是这样工作的。我采用对象模型并使属性可观察。这不是故意的。
标签: json knockout.js knockout-mapping-plugin