I have an API .get call in Node.js that retrieves all of the Messages to a specific user.
.get(function(req, res) {
console.log(req.session.username);
Models.Message.find( { "to.username": req.session.username } ).exec(function (err, messages) {
if (err)
res.send(err);
res.json(messages);
});
These Messages are delivered via res.json(messages); over AJAX to a handlebars templating script.
function allTheDocs(resJSON) {
var templateSource = $("#messagesTemplate").html();
var template = Handlebars.compile(templateSource);
var messagesHTML = template({messages: resJSON});
$('#messages').html(messagesHTML);
}
In the script I would like to call a custom comparison helper, such that if a field:value of the MongoDB data === the session/local variable in node.js -> the templated HTML then renders.
Handlebars.registerHelper('ifEqual', function(v1, v2, options) {
if (v1 === v2) {
return options.fn(this);
}
return options.inverse(this);
});
How would I send either an express session variable (req.session.username) or a local variable (res.locals.username) along with the res.json(messages), to then specifically template the unread Messsages for the session user?
{{#each messages}}
{{#ifEqual to.username req.session.username}}
{{#if read.marked}
<div class="unreadMessages">{{../message}}</div>
{{else}
<div class="readMessages">{{../message}}</div>
{{/if}}
{{/if}}
{{/each}}
Messages are marked unread in a MessageUserSchema that is an embedded array in each Message, so as to account for the option of being sent To any number of users (one to many).
{
"_id" : ObjectId("53b203c2cc3060000000eadc"),
"message" : "Here's a sample Message",
"to" : [
{
"user" : ObjectId("53aada6f8b10eb0000ec8a90"),
"username" : "username1",
"updated" : ISODate("2014-07-01T05:46:22Z"),
"_id" : ObjectId("53b24b2e35eaa0a4106ca21c"),
"read" : {
"marked" : false
}
}
]
}
Hello World
{{/if}} doesn't render.. Any other suggestions? – StackThis Jul 01 '14 at 19:09