Skip to content

Instantly share code, notes, and snippets.

@yoamomonstruos
Created December 23, 2014 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoamomonstruos/4ae1433e678bbc40df13 to your computer and use it in GitHub Desktop.
Save yoamomonstruos/4ae1433e678bbc40df13 to your computer and use it in GitHub Desktop.
TableView.js
var TableView = {
setup: {
collection: function () {
var collection = this.data.collection || this.data.settings.collection || this.data,
filtration = this.data.crossfilter,
filters = this.data.filters.get() || {},
_this = this,
result;
if (!(collection instanceof Meteor.Collection) && _.isFunction(collection.fetch)) {
result = new Meteor.Collection(null);
result._transform = collection._transform;
result._name = collection._name;
collection.observe({
added: function (doc) {
result.insert(doc);
filtration.add([doc]);
if (_.size(filters) > 1) {
_.each(filters, function (filter) {
if (filter.field) {
filter.options = filter.group().all()
}
});
}
_this.data.filters.set(filters);
},
updated: function (doc, oldDoc) {
result.update(oldDoc._id, doc);
filters.id.cf.filter(oldDoc._id).top(1);
filtration.remove();
_.each(filters, function (filter) { filter.dispose(); });
filtration.add(doc);
},
removed: function (oldDoc) {
result.remove(oldDoc._id);
filters.id.filter(oldDoc._id).top(1);
filtration.remove();
_.each(filters, function (filter) {
filter.filter(null);
if (filter.field) {
filter.dispose();
filter.options = filtration.dimension(function(data) { return data[filter.field.key] }).group().all()
console.log(filter.options);
}
});
_this.data.filters.set(filters);
}
});
}
else {
throw new Meteor.Error('TableViewError',
'You need to pass in either an instance of Meteor.Collection cursor');
}
this.data.collection = result;
},
crossfilter: function () {
this.data.crossfilter = crossfilter([]);
this.data.filters = new ReactiveVar({
id: this.data.crossfilter.dimension(function (data) { return data._id })
});
},
filters: function () {
var fieldsWithFilters = _.where(this.data.settings.fields, { filter: true }),
currentFilters = this.data.filters.get(),
_this = this;
if (fieldsWithFilters.length) {
_.each(fieldsWithFilters, function (field) {
var filter = _this.data.crossfilter.dimension(function (data) { return data[field.key]; });
currentFilters[field.key] = filter;
_.extend(currentFilters[field.key], {
field: field,
options: filter.group().all()
});
});
}
this.data.filters.set(currentFilters);
}
}
}
/*****************************************************************************/
/* TableView: Event Handlers and Helpersss .js*/
/*****************************************************************************/
Template.TableView.events({
});
Template.TableView.helpers({
hasFilters: function () {
return _.size(this.filters.get()) > 0 ? true : false;
},
filtersAsArray: function () {
return _.toArray(this.filters.get());
},
tableRows: function () {
this.filters.get().type.filter('hyperlink').group().size();
return this.crossfilter.size();
}
});
/*****************************************************************************/
/* TableView: Lifecycle Hooks */
/*****************************************************************************/
Template.TableView.created = function () {
var _this = this;
TableView.setup.crossfilter.call(this);
TableView.setup.collection.call(this);
TableView.setup.filters.call(this);
// console.log(this.data.filters);
};
Template.TableView.rendered = function () {
};
Template.TableView.destroyed = function () {
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment