Files
old-ribica/front-ui/app/models/itemCollection.js

81 lines
2.1 KiB
JavaScript

var Backbone = require('backbone'),
Item = require('./item'),
Globals = require('../globals');
var ItemCollection = Backbone.Collection.extend({
initialize: function() {
$.ajaxPrefilter(
function(options, originalOptions, jqXHR) {
options.xhrFields = {
withCredentials: true
}
}
);
},
setTotalCount: function(total) {
this.totalCount = total;
},
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', 'item_group'],
setClassificationType: function(type) {
this.classificationType = type;
},
setClassificationId: function(id) {
this.classificationId = id;
},
setFromCart: function(fromCart) {
this.fromCart = fromCart;
},
model: Item,
url: function() {
if (this.fromCart === true) {
return Globals.ApiUrl + "/cart/item/display";
}
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;