1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-10 11:35:02 -05:00
kaiwa/clientapp/models/baseCollection.js
2013-08-29 20:38:28 -07:00

51 lines
1.2 KiB
JavaScript

"use strict";
// 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);
}
});