Forgive my lack of knowledge in javascript, but I am struggling to understand why this code is not functioning properly. How can I modify it to achieve the desired outcome?
// Client-side Code
Template.tabs.title = function () {
var msg;
Meteor.call('getMessage', this.msg_id, function (error, result) {
console.log(result); // Displays a valid message object
msg = result;
});
if (msg)
return msg.title;
else
return "(empty)";
};
I suspect that the "if" statement is being executed before the callback can populate the msg variable. Is this due to scoping issues? Can I make calls like this directly from the template?
The rationale behind using methods here is that the Messages collection may be significantly large, making it impractical to subscribe to all data on the client side. While I have subscriptions for specific portions of data, there is also a need to retrieve individual messages regardless of subscribed content.