Saad
Saad

Reputation: 28486

access data from iron-router in rendered function

I'm trying to access data passed from iron router in the javascript function

router.js

this.route('editOrganization', {
    path: '/editOrganization',
    waitOn: function() {
      return [
        Meteor.subscribe('organization', this.userId)
      ];
    },
    data: function() {
        return Organizations.findOne();
    }
});

now if I wanted to access a property of organization in html (editCompany.html) I can do the following

{{name}}

but how do I access that same property in the js file

Template.editOrganization.rendered = function() {
    //how do I access name?
}

UPDATE: so if I click a link to edit organization I can get the value via

this.data.name

However, if I reload the page (same url) it throws an error saying data is null.

Upvotes: 1

Views: 350

Answers (1)

Neil
Neil

Reputation: 2147

It is accessible through the rendered function context.

Template.editOrganization.rendered = function() {
    var name = this.data && this.data.name;
};

This is confusing for many people but you need to configure the router to actually wait for the subscriptions you returned with waitOn.

Router.onBeforeAction('loading')

You can read the author's explanation here:

https://github.com/EventedMind/iron-router/issues/554#issuecomment-39002306

Upvotes: 2

Related Questions