Series Index can now be filtered and no longer fetches twice when starting.

This commit is contained in:
Taloth Saldono
2014-02-01 23:09:19 +01:00
parent 953e59d7e3
commit af4c351428
7 changed files with 183 additions and 45 deletions
+78
View File
@@ -0,0 +1,78 @@
'use strict';
define(
[
'underscore',
'backbone'],
function (_, Backbone) {
return function () {
this.prototype.setFilter = function(filter, options) {
options = _.extend({ reset: true }, options || {});
this.state.filterKey = filter[0];
this.state.filterValue = filter[1];
if (options.reset) {
if (this.mode != 'server') {
this.fullCollection.resetFiltered();
} else {
return this.fetch();
}
}
};
this.prototype.setFilterMode = function(mode, options) {
this.setFilter(this.filterModes[mode], options);
};
var originalMakeFullCollection = this.prototype._makeFullCollection;
this.prototype._makeFullCollection = function (models, options) {
var self = this;
self.shadowCollection = originalMakeFullCollection.apply(this, [models, options]);
var filterModel = function(model) {
if (!self.state.filterKey || !self.state.filterValue)
return true;
else
return model.get(self.state.filterKey) === self.state.filterValue;
};
self.shadowCollection.filtered = function() {
return this.filter(filterModel);
};
var filteredModels = self.shadowCollection.filtered();
var fullCollection = originalMakeFullCollection.apply(this, [filteredModels, options]);
fullCollection.resetFiltered = function(options) {
Backbone.Collection.prototype.reset.apply(this, [self.shadowCollection.filtered(), options]);
};
fullCollection.reset = function (models, options) {
self.shadowCollection.reset(models, options);
self.fullCollection.resetFiltered();
};
return fullCollection;
};
_.extend(this.prototype.state, {
filterKey : null,
filterValue : null
});
_.extend(this.prototype.queryParams, {
filterKey : 'filterKey',
filterValue : 'filterValue'
});
return this;
};
}
);
+12 -15
View File
@@ -62,23 +62,20 @@ define(
return '1';
};
var originalMakeComparator = this.prototype._makeComparator;
this.prototype._makeComparator = function (sortKey, order, sortValue) {
var state = this.state;
_.extend(this.prototype, {
initialSort: function () {
var key = this.state.sortKey;
var order = this.state.order;
sortKey = sortKey || state.sortKey;
order = order || state.order;
if (this[key] && this.mode === 'client') {
var sortValue = this[key];
this.setSorting(key, order, { sortValue: sortValue });
var comparator = this._makeComparator(key, order, sortValue);
this.fullCollection.comparator = comparator;
this.fullCollection.sort();
}
}
});
if (!sortKey || !order) return;
if (!sortValue && this[sortKey]) sortValue = this[sortKey];
return originalMakeComparator.call(this, sortKey, order, sortValue);
};
return this;
};