changed itemcollection to support filtering by traits
This commit is contained in:
@@ -4,10 +4,11 @@ var ItemConstants = require('../constants/itemConstants');
|
|||||||
// Define action methods
|
// Define action methods
|
||||||
var ItemActions = {
|
var ItemActions = {
|
||||||
|
|
||||||
loadByCategory: function(categoryId) {
|
loadByCategory: function(categoryId, query) {
|
||||||
AppDispatcher.handleAction({
|
AppDispatcher.handleAction({
|
||||||
actionType: ItemConstants.LOAD_BY_CATEGORY,
|
actionType: ItemConstants.LOAD_BY_CATEGORY,
|
||||||
categoryId : categoryId
|
categoryId : categoryId,
|
||||||
|
query : query
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ var ByCategory = React.createClass({
|
|||||||
|
|
||||||
var q = {};
|
var q = {};
|
||||||
q[fc.field_name] = fcv.filter_value;
|
q[fc.field_name] = fcv.filter_value;
|
||||||
// TODO: fix this, clean this up
|
|
||||||
var section = new Section(this.state.category.get('section'));
|
var section = new Section(this.state.category.get('section'));
|
||||||
var category = this.state.category;
|
var category = this.state.category;
|
||||||
NavigationActions.goToCategory(category, section, q)
|
NavigationActions.goToCategory(category, section, q)
|
||||||
@@ -70,12 +69,13 @@ var ByCategory = React.createClass({
|
|||||||
},
|
},
|
||||||
componentWillReceiveProps: function() {
|
componentWillReceiveProps: function() {
|
||||||
var categoryId = this.getParams().id;
|
var categoryId = this.getParams().id;
|
||||||
ItemActions.loadByCategory(categoryId);
|
ItemActions.loadByCategory(categoryId, this.getQuery());
|
||||||
CategoryActions.loadCategoryDetails(categoryId);
|
CategoryActions.loadCategoryDetails(categoryId);
|
||||||
},
|
},
|
||||||
componentDidMount: function() {
|
componentDidMount: function() {
|
||||||
var categoryId = this.getParams().id;
|
var categoryId = this.getParams().id;
|
||||||
ItemActions.loadByCategory(categoryId);
|
ItemActions.loadByCategory(categoryId, this.getQuery());
|
||||||
|
|
||||||
CategoryActions.loadCategoryDetails(categoryId);
|
CategoryActions.loadCategoryDetails(categoryId);
|
||||||
|
|
||||||
ItemStore.addChangeListener(this._onChange);
|
ItemStore.addChangeListener(this._onChange);
|
||||||
|
|||||||
@@ -4,6 +4,10 @@ var Backbone = require('backbone'),
|
|||||||
|
|
||||||
var ItemCollection = Backbone.Collection.extend({
|
var ItemCollection = Backbone.Collection.extend({
|
||||||
|
|
||||||
|
addFilter : function(name, value) {
|
||||||
|
this.filters = this.filters || {};
|
||||||
|
this.filters[name] = value;
|
||||||
|
},
|
||||||
setLimit: function(limit) {
|
setLimit: function(limit) {
|
||||||
this.queryLimit = limit;
|
this.queryLimit = limit;
|
||||||
},
|
},
|
||||||
@@ -28,10 +32,24 @@ var ItemCollection = Backbone.Collection.extend({
|
|||||||
if(this.classificationType > 0) {
|
if(this.classificationType > 0) {
|
||||||
// eg. http://localhost:4567/item/section/1/offset/0/limit/10
|
// eg. http://localhost:4567/item/section/1/offset/0/limit/10
|
||||||
var urlPart = this.classificationTypeUrlParts[this.classificationType];
|
var urlPart = this.classificationTypeUrlParts[this.classificationType];
|
||||||
path += "/" + urlPart + "/" + this.classificationId
|
path += "/" + urlPart + "/" + this.classificationId;
|
||||||
} // else eg. http://localhost:4567/item/offset/0/limit/10
|
} // else eg. http://localhost:4567/item/offset/0/limit/10
|
||||||
path += "/offset/" + this.offset + "/limit/" + this.queryLimit;
|
path += "/offset/" + this.offset + "/limit/" + this.queryLimit;
|
||||||
return Globals.ApiUrl + path;
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -51,13 +51,19 @@ var fetchItemWithDetails = function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var fetchItemsByCategory = function(categoryId) {
|
var fetchItemsByCategory = function(categoryId, query) {
|
||||||
var items = _itemsByCategory;
|
var items = _itemsByCategory;
|
||||||
items.setClassificationType(2);
|
items.setClassificationType(2);
|
||||||
items.setClassificationId(categoryId);
|
items.setClassificationId(categoryId);
|
||||||
items.setLimit(30);
|
items.setLimit(30);
|
||||||
items.setOffset(0);
|
items.setOffset(0);
|
||||||
|
|
||||||
|
for(var key in query) {
|
||||||
|
if (query.hasOwnProperty(key)) {
|
||||||
|
items.addFilter(key, query[key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
items.fetch({
|
items.fetch({
|
||||||
success: function() {
|
success: function() {
|
||||||
ItemStore.emitChange();
|
ItemStore.emitChange();
|
||||||
@@ -140,7 +146,7 @@ AppDispatcher.register(function(payload) {
|
|||||||
loadItemsForFrontpage();
|
loadItemsForFrontpage();
|
||||||
break;
|
break;
|
||||||
case ItemConstants.LOAD_BY_CATEGORY:
|
case ItemConstants.LOAD_BY_CATEGORY:
|
||||||
fetchItemsByCategory(action.categoryId);
|
fetchItemsByCategory(action.categoryId, action.query);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in New Issue
Block a user