使用 KnockoutJs 时如何有效地持久保存数据的创建/更新/删除?
我一直在玩knockoutjs,到目前为止我已经看到了一个项目列表。
我可以使用各种按钮更新、创建和删除它们。
我还能够保存,当发生这种情况时,我的视图模型会以 json 形式发送到服务器,然后我在那里处理它。
我担心此时它的效率会稍微低下。
我目前有一个 foreach 循环来查看每个项目。
然后我一次查询每个项目 1 ,如果它不存在,我假设它是新的并添加它,如果它有 _destroy = true,我删除它,最后如果它确实存在,我只是用从发送回来的内容替换它服务器是保存更新的地方。
这对我来说似乎很浪费。
1)我将从视图中发送回每一行。
2)然后,即使没有任何变化,我也会在到达服务器时检索每一行。
有没有一种方法可以将行标记为“注意”并仅将这些行发送回服务器。
例如,假设我的视图中显示 4 行。 我向视图模型添加一个新模型,编辑现有模型并删除另一个模型。
我只想将这 3 行发送回服务器...忽略那些未更改的行。
也许我可以添加并检查类似于 _destroy 当前工作方式的特定标志?
因此,添加项目将添加一个 _create 标志,编辑将添加一个 _update 标志等。
抱歉,我希望这看起来不太像华夫饼。
您建议如何处理这个问题?
谢谢,科汉。
I have been playing round with knockoutjs and so far i have in my view a list of items.
I can update, create and delete these using various buttons.
I also have the ability to save, when this happens, my viewmodel gets sent to the server as json and i deal with it there.
I am worried that at this point it gets slightly inefficient.
I currently have a foreach loop that looks at each of the items.
I then query for each item 1 at a time, if it doesnt exist i assume it's new and add it, if it has a _destroy = true, i delete it and finally if it does exist i just replace it with what got sent back from the server this is where updates are persisted.
This seems quite wasteful to me.
1) i am sending every row back from the view.
2) i am then retrieving every row when it gets to the server even if nothing has changed.
Is there a way of flagging a row for "attention" and only sending those rows back to the server.
For example, lets say i have 4 rows displaying in my view.
I add a new one to the viewmodel, edit an existing one and delete another.
I would like to only send those 3 rows back to the server... ignoring the ones that are unchanged.
Maybe i could add and check for specific flags similar to how _destroy works currently?
So Adding items would add a _create flag and editing would add an _update flag etc.
Sorry, I hope this doesnt seem too much like waffle.
How would you suggest dealing with this?
Thanks, Kohan.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个可能有帮助的示例: http://jsfiddle.net/rniemeyer/gfkej/
它是基于此: http://www.knockmeout.net/2011/05/creating-smart-dirty-flag-in-knockoutjs.html
基本思想是为每个项目添加脏标志,然后创建一个 dependentObservable 来表示所有脏项目。过滤脏项目时,还会检查该项目是否设置了
_destroy
。添加新项目时,脏标志让您可以传入一个布尔值来指示它应立即被视为脏。在我的示例中,我只传递id === 0
,因为我将任何新项目的 id 初始化为 0。Here is a sample that might help: http://jsfiddle.net/rniemeyer/gfkej/
It is based on this: http://www.knockmeout.net/2011/05/creating-smart-dirty-flag-in-knockoutjs.html
The basic idea is to add a dirty flag to each item, then create a dependentObservable that represents all of the dirty items. When filtering for dirty items, this also checks to see if
_destroy
is set on the item. When adding a new item, the dirty flag let's you pass in a boolean to indicate that it should immediately be considered dirty. In my example, I just passid === 0
, since I am initializing the id to 0 for any new items.