gpt4 book ai didi

javascript - BackboneJS 在集合中重新排列模型同时为每个模型维护 0 索引序数属性的最佳方法

转载 作者:搜寻专家 更新时间:2023-11-01 04:44:35 24 4
gpt4 key购买 nike

我在这里遇到一个问题,我有一个 BackboneJS 模型的集合,每个模型都有一个“序数”属性来跟踪它在集合中的顺序。

这是我的播放数据

var ex_group_test_data = [{

title: 'PRE EXERCISE',
id: 0,
ordinal: 1,

group_items: [{
id: 0,
ordinal: 0,
title: 'item 1'
},{
id: 1,
ordinal: 1,
title: 'item 2'
}]

},{

title: 'MAIN PART',
id: 1,
ordinal: 0,

group_items: [{
id: 2,
ordinal: 0,
title: 'item 3',
description: 'testing descrip'
},{
id: 3,
ordinal: 1,
title: 'item 4'
}]

},{

title: 'POST EXERCISE BS',
id: 2,
ordinal: 2,

group_items: [{
id: 2,
ordinal: 0,
title: 'item 5',
description: 'testing descrip'
},{
id: 3,
ordinal: 1,
title: 'item 6'
}]

}];

这是我的主干集合的要点

        Collections.Exercise_Groups = Backbone.Collection.extend({
model: Models.Exercise_Group,
comparator: function(model){
return model.get('ordinal');
},
initialize: function(){

return this;
}

从简单开始,我希望能够采用一个模型并将其移动 +1 或 -1 序号并保持集合中所有模型的 0 索引。

最终,我想将其提升到这样一个水平,即我可以放入模型或将它们从任何位置移除并仍然保持我的 0 索引,或者采用模型并将其移动 +/- X 位置。

有人推荐了实现此目的的方法吗?

编辑 1

我已经制定了一个解决方案,我可能想在明天真正睡一觉之后优化它。它保持我收藏中模型“序数”的 0 索引,无论我是将模型相对于其原始位置向前还是向后移动。

编辑 2错误,实际上它在边缘案例上有错误。

/**
* Move model to a specified location.
*
* @param int [model id]
* @param int [mew item position]
* @return this
*/
move_to: function(m_id, new_pos){

//keep within range
if(new_pos < 0)
new_pos = 0;
else if(new_pos > (this.length - 1))
new_pos = this.length - 1;


var model = this.get(m_id),
old_pos = model.get('ordinal');


model.set({
ordinal: new_pos
});
if(new_pos == old_pos){
//trigger associated events
this.sort();
return this;
}

//update indexes of affected models
this.each(function(m){

//ordinal of current model in loop
var m_ordinal = m.get('ordinal');

//skip if this is the model we just updated
if(m.get('id') == m_id)
return;

if(old_pos < new_pos){
//moving down, ordinal is increasing

if(m_ordinal <= new_pos && m_ordinal != 0){
//this is in the range we care about
m.set({
ordinal: m.get('ordinal') - 1
});

}

}else if(old_pos > new_pos){
//moving up, ordinal is decreasing


if(m_ordinal >= new_pos && (m_ordinal != (this.length - 1))){
//this is in the range we care about
m.set({
ordinal: m.get('ordinal') + 1
});

}

}

});

this.sort();
return this;

}

编辑 3好吧,我想我已经解决了所有问题,一些简单的范围问题。这是一些我已经非常彻底地测试过的代码,我相信它可以工作。

/**
* Move model to a specified location.
*
* @param int [model id]
* @param int [mew item position]
* @return this
*/
move_to: function(m_id, new_pos){

//keep within range
if(new_pos < 0)
new_pos = 0;
else if(new_pos > (this.length - 1))
new_pos = this.length - 1;


var model = this.get(m_id),
old_pos = model.get('ordinal');

log('old_pos ' + old_pos);
log('new_pos ' + new_pos);


model.set({
ordinal: new_pos
});
if(old_pos == new_pos){
//trigger associated events
this.sort();
return this;
}

var _this = this;
//update indexes of affected models
this.each(function(m){

//ordinal of current model in loop
var m_ordinal = m.get('ordinal');

//skip if this is the model we just updated
if(m.get('id') == m_id)
return;

if(old_pos < new_pos){
//moving down, ordinal is increasing

if(m_ordinal <= new_pos && !(m_ordinal <= 0)){
//this is in the range we care about
m.set({
ordinal: m.get('ordinal') - 1
});

}

}else if(old_pos > new_pos){
//moving up, ordinal is decreasing
log('p1');

if(m_ordinal >= new_pos && !(m_ordinal >= (_this.length - 1))){
//this is in the range we care about
m.set({
ordinal: m.get('ordinal') + 1
});

}

}

});

this.sort();
return this;

}

编辑 4

发现另一个错误,修复它。

Backbone.Collection.prototype.move_to = function(m_id, new_pos) {

//keep within range
if(new_pos < 0)
new_pos = 0;
else if(new_pos > (this.length - 1))
new_pos = this.length - 1;


var model = this.get(m_id),
old_pos = model.get('ordinal');

log('old_pos ' + old_pos);
log('new_pos ' + new_pos);

model.set({
ordinal: new_pos
});
if(old_pos == new_pos){
//trigger associated events
this.sort();
return this;
}

var _this = this;
//update indexes of affected models
this.each(function(m){

log(m.id);

//ordinal of current model in loop
var m_ordinal = m.get('ordinal');

//skip if this is the model we just updated
if(m.get('id') == m_id)
return;

if(old_pos < new_pos){
//moving down, ordinal is increasing

if(m_ordinal <= new_pos && m_ordinal >= old_pos && !(m_ordinal <= 0)){
//this is in the range we care about
m.set({
ordinal: m.get('ordinal') - 1
}, {
silent: true
});

}

}else if(old_pos > new_pos){
//moving up, ordinal is decreasing
log('p1');

if(m_ordinal >= new_pos && m_ordinal <= old_pos && !(m_ordinal >= (_this.length - 1))){
//this is in the range we care about
m.set({
ordinal: m.get('ordinal') + 1
}, {
silent: true
});

}

}

});

this.sort();
return this;
};

最佳答案

如果你查看 backbone.js 源代码,你会发现例如 add 方法支持将模型添加到某些索引

collectionName.add(model, {at: index});

从位置上移除可能需要您为集合创建一个自定义函数,例如:

// Your Collection

removeAt: function(options) {
if (options.at) {
this.remove(this.at(options.at));
}
}

对于 +1/-1 你可以创建自定义函数并使用内置的下划线 indexOf 函数

// Your Collection

moveUp: function(model) { // I see move up as the -1
var index = this.indexOf(model);
if (index > 0) {
this.remove(model, {silent: true}); // silence this to stop excess event triggers
this.add(model, {at: index-1});
}
}

moveDown: function(model) { // I see move up as the -1
var index = this.indexOf(model);
if (index < this.models.length) {
this.remove(model, {silent: true}); // silence this to stop excess event triggers
this.add(model, {at: index+1});
}
}

通过这种方式,您还可以将 moveUp 和 moveDown 实现到模型本身,以获得更易于阅读的代码!

// Your Model

moveUp: function() {
this.collection.moveUp(this);
}

// And the same for moveDown

但是现在索引属性没有保存在模型本身中。要读取索引,只需使用 collection.indexOf(model),但如果您想始终将该信息存储在模型中,您可以绑定(bind)到 addremove 事件以在对集合进行更改时更新所有索引:

// Your collection

initialize: function(options?) {
...
this.on('add remove', this.updateModelOrdinals);
...
},

...

updateModelOrdinals: function() {
this.each(function(model, index) {
this.model.set('ordinal', index);
});
}

等等!现在您应该拥有所需的功能,而无需重新发明轮子,并且仍然使用 Backbone 自己的功能保持 0 索引!抱歉让你有点失控,问问我是不是越过了你的脑袋。并阅读 the backbone.js source ,您可以在那里找到非常有用的东西!

希望这对您有所帮助!

关于javascript - BackboneJS 在集合中重新排列模型同时为每个模型维护 0 索引序数属性的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11661401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com