如何更新以表单表示的 JSON 对象 [英] How to update a JSON Object that is represented in a form

查看:22
本文介绍了如何更新以表单表示的 JSON 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于创建表单的 JSON 对象.这个 JSON 对象由 KnockoutJS 解析.

现在,当我修改表单时,我希望根据表单中所做的修改来更新 JSON 对象.问题是我事先不知道表单会是什么样子,但我知道在 JSON 对象中哪些字段需要更新.

我真的不知道最好的方法是什么.我知道每次发生变化时我都可以重建 JSON 对象,但这似乎是一个坏主意和一个乏味的过程.

是否有一种简单的方法可以将每个 JSON 对象字段映射到 KnockoutJS 中的表单项?

这是我目前正在做的 JSFiddle:http://goo.gl/ZBaV7

更新:

我发现这行很有趣:

<input type="text" data-bind="value: $data.value, attr : { disabled: $data.disabled }"/>

我通过 ($data.value) 直接从数组访问值.html中有没有办法说淘汰赛绑定到数组中的这个特定属性.我知道如果数组被重新排序,一切都会变得一团糟,但因为我知道唯一可以改变的是这个属性,我准备好承担这个风险了吗?

也就是说,有没有办法手动说当这个值改变时在数组中改变它比如

data-bind="onChange: $data.value = this.value"

解决方案

是否有一种简单的方法来映射每个 JSON 对象字段以形成项目KnockoutJS ?

是的,如果我理解您想要正确执行的操作.到目前为止,您的视图模型中的值不是可观察的,并且不会随着表单值的变化而自动更新.有一个插件来处理这个映射.

http://knockoutjs.com/documentation/plugins-mapping.html

<块引用>

示例:使用 ko.mapping

要通过映射插件创建视图模型,请替换创建上面代码中的 viewModel 和 ko.mapping.fromJS 函数:

var viewModel = ko.mapping.fromJS(data);

这会自动为每个对象创建可观察的属性数据的属性.然后,每次您从服务器,您可以一步一步更新viewModel上的所有属性再次调用 ko.mapping.fromJS 函数:

ko.mapping.fromJS(data, viewModel);

希望这会有所帮助.

I have a JSON object that I used to create a form. This JSON object is parsed by KnockoutJS.

Now, when I modify the form, I want the JSON object to be updated according to the modifications made in the form. The thing is that I don't know in advance how the form will be like but I know in the JSON Object which fields need to be updated.

I really don't know what is the best way to procede. I know that I could reconstruct the JSON Object each time something has changed but this seems like a bad idea and a tedious process.

Is there a simple way to map each JSON Object field to form items in KnockoutJS ?

Here's a JSFiddle of what I'm currently doing:http://goo.gl/ZBaV7

Update :

I realized something interesting with this line:

<input type="text" data-bind="value: $data.value, attr : { disabled: $data.disabled }" />

I'm accessing the value directly from the array via ($data.value). Is there a way in the html to say to knockout to bind to this particular attribute in the array. I know that if the array would get reordered everything would get messed up but since I know that the only thing that can changed is this property I'm ready to take this risk ?

In other words, is there a way to manually say that when this value changes to change it in the array such as

data-bind="onChange: $data.value = this.value"

解决方案

Is there a simple way to map each JSON Object field to form items in KnockoutJS ?

Yes, If I understand what you want to do correctly. As of now, the values in your view model are not observables and won't be updated automatically as the form values change. There is a plugin to handle this mapping.

http://knockoutjs.com/documentation/plugins-mapping.html

Example: Using ko.mapping

To create a view model via the mapping plugin, replace the creation of viewModel in the code above with the ko.mapping.fromJS function:

var viewModel = ko.mapping.fromJS(data);

This automatically creates observable properties for each of the properties on data. Then, every time you receive new data from the server, you can update all the properties on viewModel in one step by calling the ko.mapping.fromJS function again:

ko.mapping.fromJS(data, viewModel);

Hopefully this helps.

这篇关于如何更新以表单表示的 JSON 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆