使用 Knockoutjs 即时更新后端

发布于 2025-01-05 22:01:34 字数 164 浏览 0 评论 0原文

我正在尝试创建一个带有表单的网页,一旦用户更改任何字段,验证和更新就会立即提交,而不是让用户单击提交按钮。我正在使用 Knockout.js 和映射插件。我知道我可以通过为每个原始字段创建一个计算字段来实现这一点,但这种工作既乏味又愚蠢,是否有好的做法来创建一个通用侦听器来侦听任何字段中的任何更改并分别更新后端?

I am trying to create a web page with form that once user change any field, the validation and update commit immediately rather than letting user to click on submit button. I am using Knockout.js and mapping plugin. I know I can achieve this by creating a computed field for each original fields, but this kind of work is tedius and dumb, is there good practice to create a general listener to listen on any changes in any fields and update backend respectively?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁梦 2025-01-12 22:01:34

为了订阅任何更改,您可以使用 ko.toJS() 方法。实际上,它允许遍历对象图并展开可观察量。正如您可能知道的那样,当您使用 ko.compulated 时,它会订阅可观察字段的所有读取并重新评估每个更改。因此,如果您使用这样的代码:

ko.computed(function() {
   ko.toJS(viewModel);
   // update data on server
});

另外您应该注意这段代码将在初始化后立即更新服务器上的数据。这是很容易避免的。请查看此示例: http://jsfiddle.net/UAxXa/embedded/result/

另外,我认为您可能想要仅将更改的数据发送到服务器。您可以合并 ko.editbales 插件 ( https://github.com/romanych/ko.editables ) 和一些 KO -引擎盖知识。请查看此示例:http://jsfiddle.net/romanych/RKn5k/

我希望它可以帮助您。

In order to subscribe to any change you could use ko.toJS() method. Actually it allows to walk through object graph and unwrap observables. As your probably know when you use ko.computed it subscribes to all reads of observables fields and re-evaluate on every change. So if you use code like this:

ko.computed(function() {
   ko.toJS(viewModel);
   // update data on server
});

Also you should pay attention that this piece of code will update data on server right after initialization. It could be easily avoided. Please checkout this example: http://jsfiddle.net/UAxXa/embedded/result/

Also I think you might want to send only changed data to server. You could incorporate ko.editbales plugin ( https://github.com/romanych/ko.editables ) and some KO under-hood knowledge. Please checkout this sample: http://jsfiddle.net/romanych/RKn5k/

I hope it could help you.

梦境 2025-01-12 22:01:34

您有多种选择,但如果您想要一个侦听器,一种好方法是使用我用来创建更改跟踪器的相同代码。它只是监听可观察到的变化。大约有19行代码。您可以抓住它,而不是使用它来跟踪更改,只需连接一个在更改发生时保存更改的方法即可。

要设置更改跟踪,请将此跟踪器属性添加到您的视图模型中:

viewModel.tracker = new ChangeTracker(viewModel); 

将这些挂钩到您的视图以确定何时发生更改:

viewModel.tracker().somethingHasChanged(); 

当您想要重置函数中的状态(例如:加载、保存)时,将其挂钩到您的视图模型中:

viewModel.tracker().markCurrentStateAsClean; 

您也可以选择传递自己的 hashFunction 进行状态跟踪。

You've got several options but if you want a single listener, one good way is to use the same code I used to create a change tracker. It simply listens for the observable changes. Its about 19 lines of code. You can grab this and instead of using it for change tracking, just wire in a method that saves the changes when they occur.

To Setup change tracking, add this tracker property to your view model:

viewModel.tracker = new ChangeTracker(viewModel); 

Hook these into your view to determine when changes occur:

viewModel.tracker().somethingHasChanged(); 

Hook this into your view model when you want to reset state in functions (ex: load, save):

viewModel.tracker().markCurrentStateAsClean; 

Optionally, you can pass your own hashFunction for state tracking, too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文