1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-28 12:12:26 -05:00
kaiwa/clientapp/models/baseCollection.js

51 lines
1.2 KiB
JavaScript
Raw Normal View History

2013-08-29 23:38:28 -04:00
"use strict";
2013-08-20 13:45:06 -04:00
// our base collection
var Backbone = require('backbone');
module.exports = Backbone.Collection.extend({
// ###next
// returns next item when given an item in the collection
next: function (item, filter, start) {
var i = this.indexOf(item),
newItem;
if (i === -1) {
i = 0;
} else if (i + 1 >= this.length) {
i = 0;
} else {
i = i + 1;
}
newItem = this.at(i);
if (filter && newItem !== start) {
if (!filter(newItem)) {
return this.next(newItem, filter, start || item);
}
}
return newItem;
},
// ###prev
// returns previous item when given an item in the collection
prev: function (item, filter, start) {
var i = this.indexOf(item),
newItem;
if (i === -1) {
i = 0;
} else if (i === 0) {
i = this.length - 1;
} else {
i = i - 1;
}
newItem = this.at(i);
if (filter && newItem !== start) {
if (!filter(newItem)) {
return this.prev(newItem, filter, start || item);
}
}
return this.at(i);
}
});