knockout.js中的嵌套视图模型和json
Nested viewmodels and json in knockout.js
我在这里看到的问题和我这里的问题有些相似,但它们要么比我的实现更高级,要么是不同的方向。
问题是,接收一个json字符串,其中嵌套信息如下:
{"StudentBaseData":{
"StudentGuid":123456,
"FirstName":"my name",
"LastName":"my last name",
"Email":"email@email.com",
"Password":123456,
"Birthdate":"01-01-1986",
"Picture":null,
"MobilePhone":"123456789",
"Gender":"Hr."},
"PrimaryEducation":{
"Name":"something",
"Institution":"something",
"StudyStartDate":"2011-12-01",
"GraduationDate":"2013-12-01",
"ThesisSubject":"something"},
"MAddress":{
"Street":"a road",
"StreetNr":"12",
"ZipCode":"1234",
"City":"a city"}
}
我可以将其重新包装为我可以理解的视图模型(我的淘汰赛技能非常基本,我只是学习这个),但问题是当我必须将视图模型发送回后端。这是一个web api。web api期望返回相同类型的json。
这是我当前的视图模型:
var ViewModel = {
studentGuid: ko.observable("<%=Session["guid"]%>"),
firstname: ko.observable(""),
lastname: ko.observable(""),
email: ko.observable(""),
password: ko.observable(""),
birthdate: ko.observable(""),
day: ko.observable(""),
month: ko.observable(""),
year: ko.observable(""),
picture: ko.observable(""),
mobilephone: ko.observable(""),
gender: ko.observable(""),
street: ko.observable(""),
streetnr: ko.observable(""),
zipcode: ko.observable(""),
city: ko.observable(""),
primaryEducationName: ko.observable(""),
primaryEducationInstitution: ko.observable(""),
primaryEducationStudyStartDate: ko.observable(""),
primaryEducationGraduationDate: ko.observable(""),
primaryEducationThesisSubject: ko.observable("")
};
就像我说的,很简单。但问题是如何复制筑巢。在视图模型中像这样处理可观察对象是行不通的:
StudentBaseData.firstname: ko.observable(""),
StudentBaseData.lastname: ko.observable(""),
StudentBaseData.email: ko.observable(""),
"StudentBaseData.firstname": ko.observable(""),
"StudentBaseData.lastname": ko.observable(""),
"StudentBaseData.email": ko.observable(""),
然后我看到了这样的东西:
StudentBaseData[
lastname: ko.observable(""),
email": ko.observable("")
]
那也不行。
我该怎么办?
应该可以:
var ViewModel = {
StudentBaseData: {
studentGuid: ko.observable("<%=Session["guid"]%>"),
firstname: ko.observable(""),
lastname: ko.observable(""),
email: ko.observable(""),
password: ko.observable(""),
birthdate: ko.observable(""),
day: ko.observable(""),
month: ko.observable(""),
year: ko.observable(""),
picture: ko.observable(""),
mobilephone: ko.observable(""),
gender: ko.observable(""),
},
MAddress: {
street: ko.observable(""),
streetnr: ko.observable(""),
zipcode: ko.observable(""),
city: ko.observable(""),
},
PrimaryEducation: {
educationName: ko.observable(""),
educationInstitution: ko.observable(""),
educationStudyStartDate: ko.observable(""),
educationGraduationDate: ko.observable(""),
educationThesisSubject: ko.observable("")
}
};
在你的html中:
<span data-bind="text: PrimaryEducation.educationName"></span>
相关文章:
- Ext.js从json构建模型关系的问题
- 如何将本地json数据加载到Extjs数据模型中
- 尝试JSON.stringify Ember模型时Id丢失
- 将json模式转换为角度树控制树模型
- Ember数据帮助!无法将JSON反序列化为模型
- Webgl 加载 json 模型问题
- 如何将多个 Blender JSON 模型合并到一个 Three.js Object3D 对象中
- 三.js:如何从Web-worker加载动画.json模型
- Django 使用 JSON 获取模型方法的值
- 如何使用setVisible()方法设置json模型中元素的可见属性
- 通过json动态绑定作用域到ng模型
- 主干模型通过ID获取JSON元素
- 通过JSON文件设置骨干模型
- 循环访问 JSON 对象并收集模型值
- 筛选 json 并创建主干模型和集合
- 使用Spine.Model.Ajax,如何在模型更新时处理响应的JSON中的额外属性
- 如何将 int 与 int 模型 json 和 mvc 4 进行比较
- 使用 Ember.js 在模板中显示模型 JSON 数据
- EXTjs网格模型json-noob
- 一个应用程序中的JSON和OData模型.JSON模型绑定向OData发送请求