admin管理员组文章数量:1310969
Let's say I have a collection in backbone and I want to be able to find the previous and next elements for a given element. Please assume that elements can be dynamically deleted and added.
var MyModel=Backbone.Model.extend({
nextElement: function(){
//????
},
previousElement:function(){
//?????
}
});
var MyCollectionType=Backbone.Collection.extend({
model:MyModel;
});
var collection=new MyCollectionType
Let's say I have a collection in backbone and I want to be able to find the previous and next elements for a given element. Please assume that elements can be dynamically deleted and added.
var MyModel=Backbone.Model.extend({
nextElement: function(){
//????
},
previousElement:function(){
//?????
}
});
var MyCollectionType=Backbone.Collection.extend({
model:MyModel;
});
var collection=new MyCollectionType
Share
Improve this question
edited Apr 9, 2012 at 20:51
jwueller
31k5 gold badges67 silver badges70 bronze badges
asked Apr 9, 2012 at 20:45
JoeJoe
8,04218 gold badges57 silver badges86 bronze badges
2
- 3 Shouldn't you be asking the collection for the information? – mu is too short Commented Apr 9, 2012 at 21:07
- It would probably better to put it on the collection. But if you are writing something small, it can go either way. – Joe Commented Apr 10, 2012 at 3:55
2 Answers
Reset to default 13When a model is added to a collection, the collection
property is added to the model which references the collection it is in. You can use this property in the nextElement and previousElement methods.
var MyModel = Backbone.Model.extend({
initialize: function() {
_.bindAll(this, 'nextElement', 'previousElement');
},
nextElement: function() {
var index = this.collection.indexOf(this);
if ((index + 1) === this.collection.length) {
//It's the last model in the collection so return null
return null;
}
return this.collection.at(index + 1);
},
previousElement: function() {
var index = this.collection.indexOf(this);
if (index === 0 ) {
//It's the first element in the collection so return null
return null;
}
return this.collection.at(index - 1);
}
}
However, it seems that nextElement
and previousElement
are concerns the collection should have and not the model. Have you considered putting these functions on the collection rather than the model?
It was discussed as a backbone issue
https://github./jashkenas/backbone/issues/136
linssen
It can be something like this You can always get around this using a new model method:
getRelative: function(direction) {
return this.collection.at(this.collection.indexOf(this) + direction);
}
So if you pass -1 it'll get the previous and 1 will get you the next. It'll return -1 if it doesn't find anything.
本文标签: javascriptGetting the next and previous elements in backbonejsStack Overflow
版权声明:本文标题:javascript - Getting the next and previous elements in backbone.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741852258a2401139.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论