Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用主干.js。我的集合中的元素只是按照它们添加的顺序。但是,我希望能够切换元素的位置。你怎么做到这一点?
你可以这样做:
var MyCollection = Backbone.Collection.extend({ swapItems : function(index1, index2) { this.models[index1] = this.models.splice(index2, 1, this.models[index1])[0]; } });
这直接访问模型数组,然后将交换索引指定的项目。