Skip to content

Commit

Permalink
Rerun function arguments to ReactiveTable.publish when user changes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
aslagle committed Feb 18, 2015
1 parent 59458f0 commit d4ce0af
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
21 changes: 12 additions & 9 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
ReactiveTable = {};

ReactiveTable.publish = function (name, collection, selector, settings) {
if (collection) {
ReactiveTable.publish = function (name, collectionOrFunction, selectorOrFunction, settings) {
Meteor.publish("reactive-table-" + name, function (publicationId, filter, fields, options, rowsPerPage) {
if (_.isFunction(collection)) {
collection = collection.call(this);
var collection;
var selector;

if (_.isFunction(collectionOrFunction)) {
collection = collectionOrFunction.call(this);
} else {
collection = collectionOrFunction;
}

if (!(collection instanceof Mongo.Collection)) {
console.log("ReactiveTable.publish: no collection to publish");
return [];
}

if (_.isFunction(selector)) {
selector = selector.call(this);
if (_.isFunction(selectorOrFunction)) {
selector = selectorOrFunction.call(this);
} else {
selector = selectorOrFunction;
}
var self = this;
var filterQuery = _.extend(getFilterQuery(filter, fields, settings), selector);
Expand Down Expand Up @@ -86,7 +92,4 @@ ReactiveTable.publish = function (name, collection, selector, settings) {
handle.stop();
});
});
} else {
return [];
}
};
5 changes: 4 additions & 1 deletion package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package.describe({
summary: "A reactive table designed for Meteor",
version: "0.6.7",
version: "0.6.8",
name: "aslagle:reactive-table",
git: "https://github.com/aslagle/reactive-table.git"
});
Expand Down Expand Up @@ -49,9 +49,12 @@ Package.on_test(function (api) {
api.add_files('test/test_settings.js', 'client');
api.add_files('test/test_fields_tmpl.html', 'client');
api.add_files('test/test_fields.js', 'client');

api.use('[email protected]', ['client', 'server']);
api.add_files('test/test_reactivity_server.js', 'server');
api.add_files('test/test_reactivity.html', 'client');
api.add_files('test/test_reactivity.js', 'client');

api.add_files('test/test_sorting.js', 'client');
api.add_files('test/test_filtering_server.js', 'server');
api.add_files('test/test_filtering.js', 'client');
Expand Down
31 changes: 31 additions & 0 deletions test/test_reactivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,34 @@ testAsyncMulti('Reactivity - server-side collection', [function (test, expect) {
Meteor.setTimeout(expectInitialRow, 500);
}]);

testAsyncMulti('Reactivity - server-side collection access', [function (test, expect) {
var table = Blaze.renderWithData(
Template.reactiveTable,
{collection: 'reactivity-test-access', fields: ['name', 'value']},
document.body
);

var expectDataHidden = expect(function () {
test.length($('.reactive-table tbody tr'), 0, "table should remove row");
Blaze.remove(table);
});

var expectData = expect(function () {
test.length($('.reactive-table tbody tr'), 1, "table should show one row");
Meteor.logout(function () {
Meteor.setTimeout(expectDataHidden, 500);
});
});

var expectNoData = expect(function () {
test.length($('.reactive-table tbody tr'), 0, "table should initially have no rows");
Meteor.loginWithPassword('abcd', 'abcd1234', function () {
Meteor.setTimeout(expectData, 500);
});
});

Meteor.logout(function () {
Meteor.setTimeout(expectNoData, 500);
});
}]);

17 changes: 17 additions & 0 deletions test/test_reactivity_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,20 @@ Meteor.methods({
collection.update({name: 'item 1'}, {'$set': {value: 2}});
}
});

var collection2 = new Mongo.Collection('reactivity-test-access');
collection2.remove({});
collection2.insert({name: 'item 1', value: 1});

if (!Meteor.users.findOne()) {
Accounts.createUser({username: 'abcd', password: 'abcd1234'});
}


ReactiveTable.publish('reactivity-test-access', function () {
if (this.userId) {
return collection2;
} else {
return [];
}
});

0 comments on commit d4ce0af

Please sign in to comment.