var Backbone = require('backbone'), Item = require('./item'), Globals = require('../globals'); var ItemCollection = Backbone.Collection.extend({ addFilter : function(name, value) { this.filters = this.filters || {}; this.filters[name] = value; }, clearFilter: function() { this.filters = []; }, setLimit: function(limit) { this.queryLimit = limit; }, setOffset: function(offset) { this.offset = offset; }, classificationTypeUrlParts: ['','section','category','sub_category'], setClassificationType: function(type) { this.classificationType = type; } , setClassificationId: function(id) { this.classificationId = id; }, model: Item, url: function() { var path = '/item' if(this.classificationType > 0) { // eg. http://localhost:4567/item/section/1/offset/0/limit/10 var urlPart = this.classificationTypeUrlParts[this.classificationType]; path += "/" + urlPart + "/" + this.classificationId; } // else eg. http://localhost:4567/item/offset/0/limit/10 path += "/offset/" + this.offset + "/limit/" + this.queryLimit; var queryParts = []; for(var key in this.filters) { if (this.filters.hasOwnProperty(key)) { queryParts.push(key + '=' + this.filters[key]); } } var query = ''; if (queryParts.length > 0) { query = '?' + queryParts.join('&'); } return Globals.ApiUrl + path + query; } }); module.exports = ItemCollection;