简体   繁体   English

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

[英]Best way to update CollectionView content after jQuery UI Sortable?

I've been able to get jQuery UI Sortable working in Ember.js thanks to @ghemton's safeClone method: Ember.js + jQuery UI Draggable Clone 感谢@ ghemton的safeClone方法,我已经能够在Ember.js中运行jQuery UI Sortable: Ember.js + jQuery UI Draggable Clone

I'm having trouble letting Ember know of the change though once the sort has completed. 一旦排序完成,我很难让Ember知道这个变化。 Right now, I'm using splice as suggested in Move an array element from one array position to another to move the item in the collection to its proper place. 现在,我正在使用splice将数组元素从一个数组位置移动到另一个数组位置,以将集合中的项目移动到适当的位置。 That works fine but I'm having an issue with window scrolling. 这工作正常,但我有窗口滚动的问题。

In this jsfiddle, http://jsfiddle.net/chrisconley/g4aE6/ , if you scroll to the bottom, then reorder any of the elements, the window jumps back to the top. 在这个jsfiddle, http://jsfiddle.net/chrisconley/g4aE6/ ,如果你滚动到底部,然后重新排序任何元素,窗口跳回到顶部。 If you put a breakpoint in between propertyWillChange and propertyDidChange , you can see that Ember is removing all of the items from the view momentarily, which is causing the window to jump back to the top. 如果在propertyWillChangepropertyDidChange之间放置一个断点,您可以看到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');
  }
});

Is there a way to notify Ember of the change without it removing and replacing the entire collection? 有没有办法在没有删除和更换整个集合的情况下通知Ember?

Updated Solution: 更新方案:

(Thanks to Christopher Swasey's answer below) (感谢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);
  }
});

Full example here: http://jsfiddle.net/chrisconley/NN35P/ 这里有完整的例子: http//jsfiddle.net/chrisconley/NN35P/

You shouldn't be using JS standard library calls on objects like Array. 您不应该对像Array这样的对象使用JS标准库调用。 These calls aren't trappable by Ember's bindings/observers. Ember的绑定/观察者无法捕获这些调用。 Instead, use the APIs defined by Ember, such as 'replace', 'objectAt', etc. to interact with arrays. 相反,使用Ember定义的API,例如'replace','objectAt'等与数组交互。

In this case, you'll want to swap out #splice with #replace. 在这种情况下,您需要将#splice替换为#replace。

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

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