javascript - 在 jQuery UI Sortable 之后更新 CollectionView 内容的最佳方式?

标签 javascript jquery-ui ember.js

感谢@ghemton 的 safeClone 方法,我已经能够让 jQuery UI Sortable 在 Ember.js 中工作:Ember.js + jQuery UI Draggable Clone

排序完成后,我无法让 Ember 知道更改。现在,我正在按照 Move an array element from one array position to another 中的建议使用 splice将集合中的项目移动到适当的位置。这工作正常,但我遇到了窗口滚动问题。

在这个 jsfiddle 中,http://jsfiddle.net/chrisconley/g4aE6/ ,如果您滚动到底部,然后重新排列任何元素,窗口将跳回顶部。如果在 propertyWillChangepropertyDidChange 之间放置一个断点,您会看到 Ember 正在暂时从 View 中删除所有项目,这导致窗口跳回到顶部。

App.MySortableView = Em.CollectionView.extend({
  moveItem: function(fromIndex, toIndex){
    var items = this.get('content');
    this.propertyWillChange('content');
    item = items.splice(fromIndex, 1)[0];
    items.splice(toIndex, 0, item);
    this.propertyDidChange('content');
  }
});

有没有办法在不删除和替换整个集合的情况下通知 Ember 更改?

更新的解决方案:

(感谢 Christopher Swasey 在下方的回答)

App.MySortableView = Em.CollectionView.extend({
  moveItem: function(fromIndex, toIndex){
    var items = this.get('content');
    item = items.objectAt(fromIndex);
    items.removeAt(fromIndex);
    items.insertAt(toIndex, item);
  }
});

完整示例在这里:http://jsfiddle.net/chrisconley/NN35P/

最佳答案

你不应该在 Array 这样的对象上使用 JS 标准库调用。这些调用不能被 Ember 的绑定(bind)/观察者捕获。相反,使用 Ember 定义的 API,例如“replace”、“objectAt”等,与数组交互。

在这种情况下,您需要将#splice 替换为#replace。

关于javascript - 在 jQuery UI Sortable 之后更新 CollectionView 内容的最佳方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10298057/

相关文章:

javascript - 在 JavaScript 事件代码中对回调和参数使用匿名函数而不是命名函数有什么好处?

jquery - 如何在 jQuery/javascript 中保留子元素的绑定(bind)函数

javascript - Ember & 回复 : How to resolve nested relations in route's model function?

javascript - 如何根据条件加载外部 JavaScript

javascript - 使用javascript和django将文本文件上传到服务器中的某个文件夹

javascript - 单击任何图像打开弹出窗口并更改图像 url

jQuery 可调整大小的文本区域不起作用

Jquery UI 动画不会出现在隐藏元素上

javascript - 指定 ember 组件的目标操作

ember.js - 如何从外部调用 Controller 上的操作方法,通过单击 {{action}} 具有相同的行为