【发布时间】:2017-09-04 01:31:26
【问题描述】:
我需要一个像我的网络表单中的重复表这样的功能,并且需要以 JSON 格式存储我的数据,如下所示:
[
{
"id": 1, "name": "T01", "title": "T01 form title", "totalPoints": "total of all points for sections below",
"sections":
[
{ "section": "First section", "point": 4 },
{ "section": "Second section", "point": 5 }
]
},
{
"id": 2, "name": "T02", "title": "T02 form title", "totalPoints": "total of all points for sections below",
"sections":
[
{ "section": "First section", "point": 4 },
{ "section": "Second section", "point": 5 }
]
}
]
我正在使用淘汰赛,我在下面实现了顶层结构,但在嵌套部分中遇到了困难。
这是我构建模型的尝试,请告知使用什么选项,或者如果这不正确,请告知正确的选项:
function Form(data)
{
this.Id = ko.observable(data.Id);
this.Name = ko.observable(data.Name);
this.Title = ko.observable(data.Title);
this.Total = ko.observable(data.Total);
// Option 1, like an array
this.Sections = ko.observableArray([
{
Section: data.Section,
Point: data.Total
}
]);
// Option 2, like a function
function Sections(data) {
this.Section = ko.observable(data.Section),
this.Point = ko.observable(data.Point)
}
}
稍后我将这些数据作为模型推送到像这样的可观察数组,我可以再次推送顶层,但不能嵌套属性:
self.addForm = function () {
self.forms.push(
new Form({
Id: this.id(),
Name: this.name(),
Title: this.title(),
Total: function() // TODO
// Sections nested properties implementation
})
);
self.name("");
};
【问题讨论】:
标签: json knockout.js