Script not working after installing iron:router

Hello good people,
I have an issue/lack of knowledge using iron:router. I have these two scripts that i use to to change some text in the navbar which is the part of my main layout.

<script type="text/javascript" src="/client/jquery.teletype.min.js?9b94f5b916eb56e484b0be39377d38cf4778c26e"></script>
<script type="text/javascript" src="/client/slideshow.js?c2f96edd7ac73505bfc70c8a22f5eb00752dd82f"></script>

(I dont have them included in my html, just copyed them from chrome dev tools)

Everything worked until I installed iron router and I don’t know if I need to set up something in my router.js file or this is some bug, so any help would be great.

router.js:

Router.configure({
   
    layoutTemplate: 'layout'
});

Router.route('/', function () {
  this.render('main');
});

Can you be more specific what is not working? Are you not seeing the content of your template main or are your additional javascript files not loaded? What does the console say?

Sorry for not explaining it right in the first time.
I just started learning meteor so keep that in mind.

Console isn’t showing any errors and i checked if the scripts are being loaded and they are.
My main template is rendered normally, even my layout template is rendered except for the part in the navbar template where the script would have to kick in to print out some text( this all worked before I installed iron:route).
(note: everything else works in the navbar template)

<head>
  <title></title>
</head>

<body>
 

</body>

<template name="layout">
	 {{> navbar}}


<div class="container">
	
	{{> yield}}

</div>

 {{> footer}}
</template>

Any idea how do I debug, find out why the jquery script isn’t executing?
I’ve only did console.log debuging so far and I don’t think I can do the same thing here

It looks like you’re trying to shoehorn some non-Meteor code into Meteor. Which generally doesn’t work that well.

Without seeing the included scripts, it’s hard to say for sure, but I suspect what’s going on is that your scripts are running prior to iron:router rendering the layout template. That is, the iron:router template hasn’t rendered as of the time your scripts run, so there’s no navbar to change.

Again, hard to tell without seeing your app, but I’d try the following:

(1) Create a folder called client in your main meteor app directory and move jquery.teletype.min.js and client.slideshow.js there, if you haven’t already.

(2) If you have references to these javascript files in the head of your app, remove them. Meteor should automatically ensure that all Javascript files in the client folder of your app are available to the client (and only the client – see http://docs.meteor.com/#/full/structuringyourapp for more details about organizing your project).

(3) I’m assuming you have a navbar template that looks like this:

<template name="navbar">
   <div class="navbar">Change Me!</div>
</template>

If that’s the case, then you’ll want to run the code that modifies your navbar text in the template’s rendered function, like so:

Template.navbar.rendered = function() {
  // Replace with your own code
  this.$('.navbar').text('New Text!');
}

For more info about rendered, see http://docs.meteor.com/#/full/template_rendered.

NB: If all you’re doing is changing the text of a navbar, consider doing it reactively by just altering the data context. That is, if your navbar template looks like this:

<template name="navbar">{{ title }}</template>

Then, in iron:router, render the template with a data context with the title set appropriately (see https://github.com/iron-meteor/iron-router/blob/devel/Guide.md#rendering-templates-with-data about how to change the data context in iron:router).

1 Like

Thank you good sir, finally got it working!
The problem was that scripts run before iron:router colud render even if they we’re loaded later.
So using “Template.navbar.rendered” worked.
I assumed just putting them in the client folder would be enough.

And here is the script i was using:
teletype

Remember to put certain scripts that Meteor shouldn’t touch into the client/compatibility folder

1 Like

Thank you! this worked for me after almost losing my mind :grinning: