Accessing data in iron router


#1

Im returning/getting some data in the data method, how do I access it in my second method to get the data I need? (JS noob)

 Router.route('/post/:_id', function () {
      this.render('post', {
        data: function () {
          return Posts.findOne({_id: this.params._id});
    
        },
    
        posts: function(){
    			return	Posts.find({createdBy: ??data.createdBy??})
    		}
      });

And is there some way to access this data in my template if this is not good practice


#2

I think that you need to move that piece into a helper for your post template.
Then you can access the data scope through this.

I am not on my dev computer and haven’t tested this, just freestyle. Crossing my fingers it works.

Template.post.helpers({
    posts: function() {
         return Posts.find({createdBy: this.createdBy})
    }
})

edit:

So then in your template, if you access posts within the template it will loop through all posts created by the same author as the main one you’ve loaded. Do this with:

{{#each posts}}
...
{{/each}}

#3

It works!! Thanks!

Just one question so i might learn something here, template helpers gets access to all the data that i bring in from other sources(iron router here) into that template?
If the above is true how does it know that with this it should refer to data method from the iron:router, what if i had a data2 method aswell.
Or just point me into the right direction so that I can go read about it, really interested to learn this stuff.


#4

The iron router guide is quite thorough, have a look through there to get a better understanding.

I believe the answer to your question is that if you just create a data2 hook, that isn’t going to be recognised by the router. The data hook is special and that is where the scope for the template comes from…


#5

Oh I see, so data is a reserved name so thats why we can access it through the scope this if i got it right.


#6

Yeah that is how I would describe it.


#7

Bumping an old thread

Router.route('/post/:_id', function () {
      this.render('post', {
        data: function () {
          return Posts.findOne({_id: this.params._id});

        },

Can we access the data from above in template.created ??

Template.created = fun() {
   console.log(this)
   Session.set('key', this)
   $(.post).val(this.post)
}

Because im not getting anything back and getting on error

Exception from Tracker recompute function: Maximum call stack size exceeded
RangeError: Maximum call stack size exceeded

Which is strange since the document/object isn’t large


#8

It seems “this”, console.log(this), doesn’t refere to the data from the iron:router in Template.created but to the Blaze components, so how do we access it?
In Template.event console.log(this) is the document I want, but I can’t use it here since I have to set session on the beginning, really confused how this works.


#9

Answer lies in Template.currentData(); but i still don’t get it why “this” means 2 different things in template.created and template.events.

One more thing, in the docs it says I should use onCreated and onRendered but this doesn’t work for me, I have to use created and rendered. Using windows 0.3.0.


#10

Are you using Template.template_name.onCreated or Template.onCreated ?? It looks like you’re missing the template name in your examples.


#11

Template.name.onCreated.
I’ve got a bad habit of shortening code


#12

Not sure if this is shorthand again, but this would need to be:

Template.template_name.onCreated(function() {
   console.log(this)
   Session.set('key', this)
   $(.post).val(this.post)
});

#13

Yeah, they changed it from = function(){} to (function(){}) but they didn’t mention that change in the docs

Also this.data works too


#14

Yeah that was a change in the recent update. I think the rational was that this allows multiple packages to use the template hooks without interfering with each other.