批量 Backbone.js 事件?
我的应用程序的框架是围绕折叠骨干模型构建的,通过 websocket 发送数据并使用数据更新其他客户端上的模型。我的问题是,当某个操作连续触发 5 个更改时,我应该如何批量更新这些更新。
同步方法设置为更新任何更改,但如果我同时设置 5 个项目,我不希望它连续触发 5 次。
我想我可以对任何同步执行 setTimeout ,如果其他东西尝试在一秒钟内同步,则该同步会被清除。这看起来是最好的路线还是有更好的方法?
谢谢!
My app's framework is built around collapsing backbone models sending the data via websockets and updating models on other clients with the data. My question is how should I batch these updates for times when an action triggers 5 changes in a row.
The syncing method is set up to update on any change but if I set 5 items at the same time I don't want it to fire 5 times in a row.
I was thinking I could do a setTimeout on any sync that gets cleared if something else tries to sync within a second of it. Does this seem like the best route or is there a better way to do this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有专门用主干来完成此操作,但我过去曾在其他分布式(客户端/服务器)应用程序中完成过这种批处理命令。
其要点是,如果您认为需要,您应该从超时开始并添加批量大小以进行进一步优化。
假设您的批次大小为 10。当您将 9 个项目塞入批次中,然后用户只是坐在那里,不做任何其他事情时,会发生什么?服务器永远不会收到用户想要做的事情的通知。
超时通常对于小批量来说效果很好。但是,如果您的操作会生成大量相关命令,您可能希望对所有命令进行批处理,并在准备好后立即将它们发送出去,而不是等待计时器。时间可能会在创建命令的过程中触发,并以导致问题的方式将事物分开,等等。
希望有所帮助。
i haven't done this with backbone specifically, but i've done this kind of batching of commands in other distributed (client / server) apps in the past.
the gist of it is that you should start with a timeout and add a batch size for further optimization, if you see the need.
say you have a batch size of 10. what happens when you get 9 items stuffed into the batch and then the user just sits there and doesn't do anything else? the server would never get notified of the things the user wanted to do.
timeout generally works well to get small batches. but if you have an action that generates a large number of related commands you may want to batch all of the commands and send them all across as soon as they are ready instead of waiting for a timer. the time may fire in the middle of creating the commands and split things apart in a manner that causes problems, etc.
hope that helps.
Underscore.js 是 Backbone.js 使用的实用程序库,它具有几个用于限制回调的函数:
因此,如果您知道有 5 个项目将被更改,您可以注册如下回调:
但更有可能您不知道,并且您需要采用超时方法:
Underscore.js, the utility library that Backbone.js uses, has several functions for throttling callbacks:
So if you know there are 5 items that will be changed, you could register a callback like this:
But more likely you don't, and you'll want to go with a timeout approach: