I have a route controller for a page like as given below, which returns an object. But I am not able to find this object when the template is rendered, but when I print using console.log, the obecjt is being returned correctly to the template.
MyPageController = RouteController.extend({
template: 'myPage',
increment: 5,
commentsLimit: function() {
return parseInt(this.params.commentsLimit) || this.increment;
},
findOptions: function() {
return {sort: this.sort, limit: this.commentsLimit()};
},
subscriptions: function() {
this.myObjectSub = Meteor.subscribe('singleObject', this.params._id);
this.commentsSub = Meteor.subscribe('comments', this.params._id, this.findOptions());
},
comments: function() {
return Comments.find({myObjectId: this.params._id}, this.findOptions());
},
myObj: function() {
return MyObects.findOne(this.params._id);
},
data: function() {
var hasMore = this.comments().count() === this.commentsLimit();
return {
myObj: this.myObj(),
comments: this.comments(),
ready: this.myObjectSub.ready,
nextPath: hasMore ? this.nextPath() : null
};
},
sort: {submitted: -1, _id: -1},
nextPath: function() {
return Router.routes.myPage.path({_id:this.params._id, commentsLimit: this.commentsLimit() + this.increment});
}
});
My route is defined as such:
Router.route('/myobjects/:_id/:commentsLimit?', {
name: 'myPage',
controller: MyPageController
});
I am not able to find out why MyObject is not being found by the MyPage template, where as the comments are available. When I check using console.log on RouteController, MyObj is returned correctly.
EDIT: I can find myObj in the console when the page is loaded, so its that when the template is rendered, the myObj is still not available to the template.
So when I return data in this form, the myObj is found and loads fine.
data: function() {return MyObjects.findOne(this.params._id);}
But when I want to return more than one variables, I can find other variables like comments & nextPath but not myObj?? Why is this so?
return {
myObj: this.myObj(),
comments: this.comments(),
ready: this.myObjectSub.ready,
nextPath: hasMore ? this.nextPath() : null
};
As the obj might not be available when template loads, you can do the following to load all the object values reactively when the data is available:
Template.myPage.helpers({
_id: function() {
return this.myObj ? this.myObj._id : '';
},
title: function() {
return this.myObj ? this.myObj.title : '';
},
});