Sophie McCarrell
Sophie McCarrell

Reputation: 2871

How should I add events when using iron-router and meteor?

Before using iron-router I attached events using:

Template.templatename.events = {};

With iron-router, you pass data through the data attribute in the route, and generally don't touch the global Template object... so where should I put events? Should I continue to use Template.tempaltename.events?

Are you supposed to use the global Template object in conjunction with iron-router?

Upvotes: 4

Views: 1111

Answers (2)

Hubert OG
Hubert OG

Reputation: 19544

You confuse two things: template "class" and template "instance". With Template.name.events you create global behavior of the application: how given templates is rendered, how it reacts to data, to user events.

The data you send with router is attached to a given template instance, that is the single DOM node related object that is displayed on the web page.

So yes, you should still use Template.name.events, as well as you can use Template.name.rendered, .created, .deleted, .customDataHelper and so on.

 


 

By the way, it's better to use the full version of the events:

Template.name.events({
   ...
});

It saves you from a few problems in the long run.

Upvotes: 3

BenjaminRH
BenjaminRH

Reputation: 12172

Yeah, I'd continue to attach events like that. Iron router doesn't really change the way the UI works. However, I'd keep an eye for updates, because the new Meteor UI the dev team is working on will be ready probably within the next month or two, at which point this will all change anyway.

Upvotes: 1

Related Questions