How can I get values ​​for multiple data parameters in the routes of Meteor iron routers?

I have the following route defined for my small Meteor application:

this.route('browse-class', {
    path: '/browse/:_class',
    data: {
        theClass: function() { return this.params._class; },
        numBooks: function() { return Books.find({"class": this.params._class},{sort:{"createdAt": 1}}).count(); },
        books: function() { return Books.find({"class": this.params._class},{sort:{"createdAt": 1}}); }
    }
});

      

I am not accessing the returned data values. Namely numBooks. It should return an integer, but I can't get it to behave with the following code in my template helper:

Template.browseClass.helpers({

    booksFound: function() {
        return this.data.numBooks > 0;
    },

    theOwner: function() {
        theUser = Meteor.users.findOne({_id: this.owner});
        return theUser.username;
    }

});

      

When I console.log () the value I am comparing, it seems like it is trying to compare a function instead of a return value or something? I was a little confused.

Any thoughts would be much appreciated. Thank!

+3


source to share


1 answer


the data should be defined as a function in your route like this:

data:function(){
  var booksCursor=Books.find(...);
  return {
    theClass:this.params._class,
    numBooks:booksCursor.count(),
    books:booksCursor
  };
}

      



Then, if you specified browseClass

a route as the template, it will be displayed with the result data()

as the data context, so you can access the following properties:

Template.browseClass.helpers({
    booksFound:function(){
      return this.numBooks>0;
    }
});

<template name="browseClass">
  Number of books : {{numBooks}}
  {{#each books}}
    {{...}}
  {{/each}}
</template>

      

+3


source







All Articles