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

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

问题描述

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

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

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

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.

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

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.

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

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

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

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

更新:

我意识到这一行很有趣:

I realized something interesting with this line:

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

我通过($ data.value)直接从数组中访问该值。是否有一种方法在html中说要敲除绑定到数组中的这个特定属性。我知道如果数组会被重新排序,一切都会搞砸,但是因为我知道唯一可以改变的是这个属性我准备冒这个风险吗?

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"


推荐答案


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

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


示例:使用ko.mapping

Example: Using ko.mapping

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

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);

这会自动为数据上的每个
属性创建可观察属性。然后,每次从
服务器接收新数据时,您可以通过
再次调用viewModel上的所有属性再次调用ko.mapping.fromJS函数:

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);

希望这会有所帮助。

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

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