简体   繁体   English

使用Knockout-sortable拖动observableArray项时滞后

[英]Lag when dragging observableArray items using Knockout-sortable

I have a single page application that provides an administrative UI for seating on buses. 我有一个单页面应用程序,提供在公交车上座位的管理UI。 It periodically receives updated data from the server, which is incorporated into a Knockout view model (using observables and observable arrays). 它定期从服务器接收更新的数据,该数据被合并到Knockout视图模型中(使用可观察数据和可观察数组)。 The structure looks like this: 结构如下所示:

  • View Model 查看模型
    • Buses 公共汽车
      • Seats 座位

It uses the knockout-sortable plug-in to allow the user to drag the seats around to change their order. 它使用可拆卸的可拆卸插件,允许用户拖动座椅以改变其顺序。

For various reasons (performance among them), the data from the server is sent as diffs, and the client code merges each update into the single persistent view model rather than just blowing it away and rebuilding it with each refresh. 由于各种原因(其中的性能),来自服务器的数据作为差异发送,客户端代码将每个更新合并到单个持久视图模型中,而不是将其吹走并在每次刷新时重建它。

The test page loads an initial set of data (three buses, two with seats) and everything runs smoothly -- the seats can be reordered with no problems. 测试页面加载一组初始数据(三辆公共汽车,两辆带座位),一切运行顺畅 - 座椅可以重新排序,没有任何问题。 However, after clicking the "Add Bus" button (which simulates a refresh from the server and loads the seats for the first bus -- number 890), there is a slight lag when dragging the seats around for that particular bus. 但是,在单击“添加总线”按钮(模拟来自服务器的刷新并加载第一条总线的座位 - 编号890)后,拖动该特定总线的座位时会有轻微的延迟。 It appears to be related to the way in which those seats were added to the bus after the fact, as the seats on the other two buses can still be dragged smoothly. 这事实似乎与这些座位加入公交车的方式有关,因为其他两辆公共汽车的座位仍然可以顺利拖动。

I haven't found any performance issues in my code itself, and running the profile and timeline tools in Chrome with dragging the images just shows a lot of noise (repaints, recalculated styles, mouse move events). 我在代码本身中没有发现任何性能问题,并且在Chrome中运行配置文件和时间线工具拖动图像只会显示很多噪音(重新绘制,重新计算的样式,鼠标移动事件)。 I suspect that I'm doing something incorrectly with the Knockout observables, but I can't find it. 我怀疑我正在使用Knockout observable做错事,但我找不到它。

Demo Page 演示页面

Looks like you can either ensure that there is a "dummy" item to start with and them remove it immediately after applying bindings or a potential workaround would be to get access to the widget and set floating to true. 看起来你可以确保有一个“虚拟”项目开始,并在应用绑定后立即将其删除,或者可能的解决方法是访问窗口小部件并将floating设置为true。

Something like: sortable: {data: Seats, connectClass: false, options: { activate: setFloating } } 类似于: sortable: {data: Seats, connectClass: false, options: { activate: setFloating } }

with: 有:

setFloating: function() {
  $(this).data("uiSortable").floating = true;
}

Both seem like a workaround, but seem to make it work properly. 两者似乎都是一种解决方法,但似乎使其正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM