Files
old-ribica/front-ui/app/stores/bySubCategoryStore.js
2015-03-29 23:23:21 +02:00

171 lines
4.7 KiB
JavaScript

var AppDispatcher = require('../dispatcher/appDispatcher');
var EventEmitter = require('events').EventEmitter;
//var SubCategoryCollection = require('../models/subCategoryCollection');
var SubCategory = require('../models/subCategory');
var NavigationActions = require('../actions/navigationActions');
var BySubCategoryConstants = require('../constants/bySubCategoryConstants');
var ItemCollection = require('../models/itemCollection');
var _ = require('underscore');
var _state = {
subCategory : (new SubCategory()),
items: (new ItemCollection()),
filter: {},
pagination: {}
};
//var _categoryDetails = new Category();
//var loadSections = function() {
//var sections = new SectionCollection();
//sections.fetch({success: function() {
//sectionState.sections = sections.models;
//// change will be called automatically when
//// action is run but we need to emit it again
//// when the data arive
//// it's a bit "unfluxy" but convenient.
//// "true philosophy" would be to run another "data arrived" action
//SectionStore.emitChange();
//}});
//};
var fetchItemsBySubCategory = function(subCategoryId, offset, limit, query) {
//var items = _itemsByCategory;
query = query || {};
var items = new ItemCollection();
items.clearFilter();
items.setClassificationType(3);
items.setClassificationId(subCategoryId);
items.setLimit(limit);
items.setOffset(offset);
for(var key in query) {
if (query.hasOwnProperty(key) && key != 'limit' && key !='offset') {
items.addFilter(key, query[key]);
}
}
items.fetch({
success: function(collection, response, options) {
var total = options.xhr.getResponseHeader('x-total-count');
items.setTotalCount(total);
_state.items = items;
BySubCategoryStore.emitChange();
}});
};
var load = function(subCategoryId, offset, limit, filter) {
var subCategory = new SubCategory({id : subCategoryId});
subCategory.fetch({
success: function() {
_state.subCategory = subCategory;
fetchItemsBySubCategory(subCategoryId, offset, limit, filter);
}
});
_state.filter = filter;
//BySubCategoryStore.emitChange();
};
var handleFilterCriteriaClick = function(fc, fcv) {
_state.filter[fc.field_name] = fcv.filter_value;
setTimeout(function() {
NavigationActions.goToSubCategory(_state.subCategory, 0, _state.pagination.limit, _state.filter);
}, 0);
};
var handleRemoveAppliedFilter= function(name) {
delete _state.filter[name];
setTimeout(function() {
NavigationActions.goToSubCategory(_state.subCategory, 0, _state.pagination.limit, _state.filter);
}, 0);
};
//var loadCategoryDetails = function(categoryId) {
//var category = new Category({id : categoryId});
//category.fetch({
//success: function() {
//_categoryDetails = category;
//BySubCategoryStore.emitChange();
//}
//});
//};
//var setHovered = function(id) {
//sectionState.hoveredSection = id;
//}
// Extend SectionStore with EventEmitter to add eventing capabilities
var BySubCategoryStore = _.extend({}, EventEmitter.prototype, {
getState: function() {
return _state;
},
// Emit Change event
emitChange: function() {
console.log("Emmiting BySubCategory change!");
this.emit('change');
},
// Add change listener
addChangeListener: function(callback) {
this.on('change', callback);
},
// Remove change listener
removeChangeListener: function(callback) {
this.removeListener('change', callback);
}
});
// Register callback with AppDispatcher
AppDispatcher.register(function(payload) {
var action = payload.action;
var text;
switch(action.actionType) {
case BySubCategoryConstants.LOAD:
load(action.subCategoryId, action.offset, action.limit, action.filter);
break;
case BySubCategoryConstants.FILTER_CRITERIA_CLICK:
handleFilterCriteriaClick(action.fc, action.fcv);
break;
case BySubCategoryConstants.REMOVE_APPLIED_FILTER:
handleRemoveAppliedFilter(action.name);
break;
// Respond to SELECT_ITEM action
//case SectionConstants.LOAD_SECTIONS:
//loadSections();
//break;
//case SectionConstants.SET_SECTION_HOVER:
//setHovered(action.section.get('id'));
//break;
//case SectionConstants.UNSET_SECTION_HOVER:
//setHovered('');
//break;
//case CategoryConstants.LOAD_CATEGORY_DETAILS:
//loadCategoryDetails(action.categoryId);
//break;
default:
return true;
}
// If action was responded to, emit change event
BySubCategoryStore.emitChange();
return true;
});
module.exports = BySubCategoryStore;