Javascript 在jQuery UI可排序后更新CollectionView内容的最佳方法?
由于@ghemton的Javascript 在jQuery UI可排序后更新CollectionView内容的最佳方法?,javascript,jquery-ui,ember.js,Javascript,Jquery Ui,Ember.js,由于@ghemton的safeClone方法,我已经能够在Ember.js中使用jQuery UI Sortable: 我有麻烦让灰烬知道的变化,虽然一旦排序完成。现在,我正在使用中建议的splice,将集合中的项目移动到适当的位置。这很好,但我有一个问题与窗口滚动 在这个JSFIDLE中,如果滚动到底部,然后对任何元素重新排序,窗口将跳回顶部。如果在propertyWillChange和propertyDidChange之间放置一个断点,您可以看到Ember正在瞬间从视图中删除所有项目,这导致
safeClone
方法,我已经能够在Ember.js中使用jQuery UI Sortable:
我有麻烦让灰烬知道的变化,虽然一旦排序完成。现在,我正在使用中建议的splice
,将集合中的项目移动到适当的位置。这很好,但我有一个问题与窗口滚动
在这个JSFIDLE中,如果滚动到底部,然后对任何元素重新排序,窗口将跳回顶部。如果在propertyWillChange
和propertyDidChange
之间放置一个断点,您可以看到Ember正在瞬间从视图中删除所有项目,这导致窗口跳回顶部
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)下面的回答)
这里的完整示例:您不应该对数组之类的对象使用JS标准库调用。这些调用不能被Ember的绑定/观察者捕获。相反,使用Ember定义的API,如“replace”、“objectAt”等与数组交互
在这种情况下,您需要将“拼接”替换为“替换”。谢谢!原来的问题已经更新为一个解决方案。
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);
}
});