我想我知道你在说什么。假设您可以访问相关模型和视图,这是一种基本方法:
// assumption: this is called on "change" event, only once
re_insert_item : function(model,m_view,collection,c_view) {
// remove the model's view from the DOM
m_view.$el.remove();
// assuming there is a comparator, sort() will move the
// model into the correct position of the collection. since
// we're not doing a .remove and .add, we have to sort manually
// (as per documentation instructions)
collection.sort();
// figure out the model that, based upon its sort position, our
// edited model will come *before*
var idx = collection.indexOf(model);
var next_model = collection.at(idx+1);
// insert our model's view into the DOM right above it's neighbour
// this assumes you have some way to get a view for a given model,
// which there isn't really a "standard" way to do (it seems)
if ( next_model ) {
var next_m_view = ??? // assume you have some way to get this
next_m_view.$el.before(m_view.render().$el);
}
// this model is last in the list, so just append it to the
// collection view (assuming the collection view houses the
// master list as its $el)
else {
c_view.$el.append(m_view.render().$el);
}
}
您必须根据以下条件更改其中的一部分: a) 您计划将代码放在哪里 - 即,您将从哪里/如何获取函数参数;b) 你如何将模型和视图链接在一起。
SO上有一个(有点)类似的问题。但是,与我在上面的链接中突出显示的答案相反,如果可以避免,我宁愿不使用 DOM 遍历代码,而是依赖骨干集合的顺序。
不过,我认为这就是您要寻找的。如果没有,请尽可能具体地说明缺少的内容。