I've been playing with KnockoutJS and absolutely love how much it simplifies the design from every angle by keeping stuff from falling through the cracks. My question is what is the recommended "best practice" for saving the data back to the server? My understanding is that in a connected MVVM, the first "M" is the data layer and so the dependency tracking and notifications in the ViewModel trigger saves directly back to the data layer. In a JavaScript app, we are disconnected and selectively save back to the server using AJAX.
我一直在玩KnockoutJS,并且非常喜欢它通过防止物质从裂缝中掉落而从各个角度简化设计。我的问题是将数据保存回服务器的推荐“最佳实践”是什么?我的理解是,在连接的MVVM中,第一个“M”是数据层,因此ViewModel触发器中的依赖关系跟踪和通知直接保存回数据层。在JavaScript应用程序中,我们断开连接并使用AJAX有选择地保存回服务器。
The app I'm currently using it in is MVC3 and I absolutely get how to write a "Save" action on my controller, plop a "Save" button somewhere on my page, post the whole ViewModel to that Save action and then persist that to the database. But what about when you make a quick edit and then save it again? Or what if a save button doesn't fit the flow of the design? Instead, you want to post to the action every time a change is made on the form with no save button at all? The ideas that I've bounced around are:
我正在使用它的应用程序是MVC3,我绝对得到如何在我的控制器上编写“保存”操作,在我的页面上某处“保存”按钮,将整个ViewModel发布到该Save操作,然后坚持到数据库。但是当你进行快速编辑然后再次保存时呢?或者如果保存按钮不符合设计流程怎么办?相反,您希望每次在没有保存按钮的表单上进行更改时发布到操作?我反复提出的想法是:
- Post the whole ViewModel every time any change is made and make the Action figure out what is new and what isn't (not ideal, especially for large models, if nothing else because the data transmitted on each save would be unnecessarily large).
- 每次进行任何更改时都会发布整个ViewModel,并使Action确定新内容和非新内容(不理想,特别是对于大型模型,如果没有别的,因为每次保存时传输的数据会不必要地大)。
- Add a property to each item in the ViewModel that tracks whether it is new and/or changed since the last save. Then, grep out those items and post only those to the server (I haven't tested this, but I assume this can be done using the _destroy property, as intended for a Rails app).
- 向ViewModel中的每个项添加一个属性,用于跟踪自上次保存以来它是否是新的和/或已更改。然后,grep out那些项目并仅将这些项目发布到服务器(我没有测试过这个,但我认为这可以使用_destroy属性来完成,就像Rails应用程序一样)。
- Separate into as many smaller ViewModels as is plausible so that any pain from the first two options is minimized (this should probably be done regardless).
- 分成尽可能多的小型ViewModel,这样可以减少前两个选项的任何痛苦(无论如何都应该这样做)。
- Some other better way?
- 其他一些更好的方法?
I'm hopeful there are some good ideas out there that I haven't thought of. To be able to declaratively bind everything AND still save efficiently would be awesome.
我希望有一些我没有想过的好主意。能够声明性地绑定所有内容并且仍然有效地保存将是非常棒的。
3 个解决方案
#1
0
The only other thing i could think of is subscribing. When i first started reading your post i was thinking flags w/grep though.
我唯一能想到的就是订阅。当我第一次开始阅读你的帖子时,我正在考虑用gre / grep标记。
Edit: Better yet, ko.utils.compareArrays
looks promising.
编辑:更好的是,ko.utils.compareArrays看起来很有前途。
Here's a working example..
这是一个有效的例子..
The only thing left to do is detect changes to values of the 'retained' values. You're well on your way though.
剩下要做的唯一事情是检测“保留”值的值的变化。你很顺利。
#2
5
I just got back from Mix11 where I attended this session about Knockout.js. It might be worth your while to watch Steve Sanderson crank out a full CRUD demo.
我刚从Mix11回来,在那里我参加了关于Knockout.js的会议。看史蒂夫桑德森制作一个完整的CRUD演示可能值得您花些时间。
#3
0
You might check out the Mapping plugin for Knockout, it lets you have load up Knockout from a JSON array. If it wasn't too big, you save that array down to server on a timer (or after a change). Hope this helps, sorry if you already knew this.
您可以查看Knockout的Mapping插件,它允许您从JSON数组加载Knockout。如果它不是太大,则在计时器上(或更改后)将该数组保存到服务器。希望这有帮助,对不起,如果你已经知道这一点。
http://knockoutjs.com/documentation/plugins-mapping.html
http://knockoutjs.com/documentation/plugins-mapping.html
http://knockoutjs.com/documentation/json-data.html
http://knockoutjs.com/documentation/json-data.html
#1
0
The only other thing i could think of is subscribing. When i first started reading your post i was thinking flags w/grep though.
我唯一能想到的就是订阅。当我第一次开始阅读你的帖子时,我正在考虑用gre / grep标记。
Edit: Better yet, ko.utils.compareArrays
looks promising.
编辑:更好的是,ko.utils.compareArrays看起来很有前途。
Here's a working example..
这是一个有效的例子..
The only thing left to do is detect changes to values of the 'retained' values. You're well on your way though.
剩下要做的唯一事情是检测“保留”值的值的变化。你很顺利。
#2
5
I just got back from Mix11 where I attended this session about Knockout.js. It might be worth your while to watch Steve Sanderson crank out a full CRUD demo.
我刚从Mix11回来,在那里我参加了关于Knockout.js的会议。看史蒂夫桑德森制作一个完整的CRUD演示可能值得您花些时间。
#3
0
You might check out the Mapping plugin for Knockout, it lets you have load up Knockout from a JSON array. If it wasn't too big, you save that array down to server on a timer (or after a change). Hope this helps, sorry if you already knew this.
您可以查看Knockout的Mapping插件,它允许您从JSON数组加载Knockout。如果它不是太大,则在计时器上(或更改后)将该数组保存到服务器。希望这有帮助,对不起,如果你已经知道这一点。
http://knockoutjs.com/documentation/plugins-mapping.html
http://knockoutjs.com/documentation/plugins-mapping.html
http://knockoutjs.com/documentation/json-data.html
http://knockoutjs.com/documentation/json-data.html