Getting rid of publish()
and subscribe()
SmartQuery takes your regular Collection.find()
calls, and wraps them in a special function that automatically sets up the subscription you need for you.
Install with:
meteor add utilities:smartquery
Just call SmartQuery.create()
inside a regular template helper. For example:
Template.posts.helpers({
posts: function () {
var cursor = Posts.find({}, {limit: Session.get("postsLimit")});
return SmartQuery.create("posts", cursor);
}
}
SmartQuery.create(id, cursor, options)
id
: a unique id for this particular SmartQuery.cursor
: the cursor you want to use (e.g.Posts.find({}, {limit: 5})
).options
: an optional options object.
Returns an object with the following properties:
subscription
: the subscription created for this SmartQuery.cursor
: the cursor you passed to the SmartQuery.isReady()
: (function) whether the subscription is ready (Boolean).count()
: (function) returns a count of the cursor.totalCount()
: (function) returns the total count for the cursor on the server (without alimit
).hasMore()
: (function) whether there are more documents on the server (Boolean).
You must define a security rule on the server for each collection before data can be published to users.
SmartQuery.addRule(collection, rule)
collection
: the collection.rule
: the rule.
A rule has the following properties:
filter(document)
: a function that, if provided, must returntrue
for every single document in the cursor. Inside the filter function, you can callthis.userId
to access the current user's_id
.fields()
: a function that, if provided, must return an array of publishable fields. If nofields
function is provided, all fields will be published.
In both the filter
and fields
function, you can access the current user _id
with this.userId
.
Example:
SmartQuery.addRule(Posts, {
filter: function (document) {
return document.published === true;
},
fields: function () {
return ["_id", "title", "body"];
}
});
You can also use SmartQuery with SubsManager:
var mySub = new SubsManager();
var options = {
subscribe: mySub.subscribe.bind(mySub)
};
SmartQuery.create("the-id", Posts.find(), options);