To incorporate this logic, you have the option to utilize the findOne()
function. This function is designed to locate the initial document that matches the specified selector/query object within the arguments. Essentially, by employing findOne()
with a Mongo selector, which serves as an object outlining the mandatory attributes of the desired document, you can identify a matching document. For instance, the following selector:
var doc = Model.findOne({ canDrive: false });
would correspond with this particular document:
{
_id: "01",
name: "Jimmy",
canDrive: false
}
Incorporating this approach into your template function allows for the assessment of document existence and field presence. It's important to note that findOne()
returns null if it fails to discover a matching document - a common scenario when the document hasn't been loaded or has been eliminated from the collection:
Template.profile.rendered = function() {
var doc = Model.findOne({ canDrive: false });
if (doc && !doc.canDrive){
$('.driving-tutorial').show();
else {
$('.driving-tutorial').hide();
}
}
An alternate method involves utilizing the second version of the jQuery toggle()
function, which accepts a Boolean parameter. When this parameter is set to true
, the matched elements are displayed; alternatively, setting it to false
results in the elements being hidden:
Template.profile.rendered = function() {
var doc = Model.findOne({ canDrive: false }),
canDrive = (doc && !doc.canDrive);
$('.driving-tutorial').toggle(canDrive);
}