javascript - Necessary to pass limit to data when subscription already limits the dataset? -


in example pagination, different number passed route in terms of how many results should displayed.

//   lib/router.js router.route('/:postslimit?', {   name: 'postslist',   waiton: function() {     var limit = parseint(this.params.postslimit) || 5;      return meteor.subscribe('posts', {sort: {submitted: -1}, limit: limit});   },   data: function() {     var limit = parseint(this.params.postslimit) || 5;      return {       posts: posts.find({}, {sort: {submitted: -1}, limit: limit})     };   } }); 

i wondering if it's necessary still give posts.find() arguments in data attribute. seems kinda redundant here because subscribe in waiton limit dataset getting server. know data used provide data context different parts of templates. however, providing same arguments here data posts.find() seems redundant. have tried using posts.find() without argument , worked.

also, wondering way access data in router? accessing posts collections outside individual route , thought able access data in posts collections. however, returned count 0.

//    lib/router.js var totalpostscount = posts.find().count() console.log('total count?') console.log(totalpostscount)       //  output 0   postslistcontroller = routecontroller.extend({   template: 'postslist',   increment: 5,    postslimit: function() {      return parseint(this.params.postslimit) || this.increment;    },   findoptions: function() {     return {sort: {submitted: -1}, limit: this.postslimit()};   },   subscriptions: function() {     this.postssub = meteor.subscribe('posts', this.findoptions());   },   posts: function() {     // console.log("is find limited subscriptions too??");     // console.log(posts.find().count())     return posts.find({}, this.findoptions());   },   data: function() {     // console.log('is data limited this?')     // console.log(posts.find().count())      var hasmore = this.posts().count() === this.postslimit();     var adjustornot = this.posts()     if (!hasmore){         var nextpath = this.route.path({postslimit: this.posts().count()});          }else{         var nextpath = this.route.path({postslimit: this.postslimit() + this.increment});            }     return {       posts: this.posts(),       ready:this.postssub.ready(),       nextpath: hasmore ? nextpath : null     };   } });  //...  router.route('/:postslimit?', {   name: 'postslist' }); 

edit 1: code getting posts:

// server meteor.publish('allposts',function(){      return posts.find({}); })   // lib/router.js if (meteor.isclient){     var handle = meteor.subscribe('allposts');     if (handle.ready()) {       // logs actual count of posts       console.log(posts.find().count());     } else {        console.log('not ready yet');     } } 

this outputs 'not ready yet' in console , it's not changing when page finish loading.

thanks lot help.

edit 2: possible solutions

i tried wrapping reactive source ready() inside computation such tracker.autorun() , worked.

if (meteor.isclient){     var handle = meteor.subscribe('allposts');      tracker.autorun(function(){         var status = handle.ready();         if (status){             console.log(posts.find().count())         }else{             console.log('not ready yet')         }     })  } 

yes, should pass limit , other query options front-end query too, regardless of subscription. because if have multiple subscriptions same collection active @ same time (as done in larger apps) subscribed records end in same minimongo collection in front-end. in case, if had omitted query parameters, end unpredictable results. see more in this excellent explanation. note while iron:router supports doing subscriptions in route handlers, it's encouraged handle them in template lifecycle methods.

for second question, reason posts collection appears empty code in top level of file run file loaded. @ point, collection subscription isn't loaded yet, front-end collection empty. need check subscription readiness in reactive context, such template helper or tracker.autorun, ensure data loaded:

var handle = meteor.subscribe('posts'); if (handle.ready()) {   // logs actual count of posts   console.log(posts.find().count()); } else {   // logs 0 (unless there other subscriptions collection)   console.log(posts.find().count()); } 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -