Destroy/remove a meteor template in iron-router - meteor

I've just started using iron router in meteor. How do I hide or delete a template, or replace it with another?
I have two client-side routes. One shows a list of chatrooms a user can join:
Router.route('/', function () {
this.layout('bodyLayout');
this.render('roomList', {
to: 'roomList'
});
});
The other is for the chatrooms:
Router.route('/room/:_id', function () {
this.layout('bodyLayout');
var roomId = this.params._id;
this.render('room', {
to: 'room',
data: () => {
return { roomId }
}
});
});
Both of these have the same layout where they are yielded close together:
<template name="bodyLayout">
<!-- layout stuff -->
{{> yield 'roomList'}}
{{> yield 'room'}}
<!-- layout stuff -->
</template>
Now, if I go to / and click a room, the room renders under it. But what I really want is for this to show either room OR roomList. How do I delete roomList when creating room, or can I replace it with room somehow?

So ultimately I've found several solutions.
Replacing a template
This is useful in a case like mine, if a certain location needs only one of several templates. It turns out that several routes can render to the same yield. we can remove one of the yields:
<template name="bodyLayout">
<!-- layout stuff -->
{{> yield }}
<!-- layout stuff -->
</template>
And remove the to attribute from the object we pass to this.render:
Router.route('/', function () {
this.layout('bodyLayout');
this.render('roomList');
});
Router.route('/room/:_id', function () {
this.layout('bodyLayout');
var roomId = this.params._id;
this.render('room', {
// We don't need this anymore:
// to: 'room',
data: () => {
return { roomId }
}
});
});
Alternatively, do give the yield a name:
<template name="bodyLayout">
<!-- layout stuff -->
{{> yield 'content' }}
<!-- layout stuff -->
</template>
And give both routers the 'to' attribute that refers to the same yield:
Router.route('/', function () {
this.layout('bodyLayout');
this.render('roomList', { to: 'content'});
});
Router.route('/room/:_id', function () {
this.layout('bodyLayout');
var roomId = this.params._id;
this.render('room', {
to: 'content',
data: () => {
return { roomId }
}
});
});
Removing a template from a router
If you do need to remove a template from a router, you can do this by giving the this.render function an empty string instead of a template name, thus telling it to render no template to this yield:
Router.route('/', function () {
this.layout('bodyLayout');
this.render('roomList', {
to: 'roomList'
});
// Remove the room that was shown
this.render('', { to: 'room'});
});

Related

Why isn't this template reactive?

Why isn't this reactive? And more importantly how can it be made reactive?
I'd like the data to be saved in Mongo and used in the template. I could use a ReactiveVar or ReactiveDict. Do I need two copies of the data?
Doesn't Suspects.findOne('bruce') return a reactive object already? I tried putting the human answer directly on Bruce, but it didn't trigger an update.
The events fire, log(this) shows bruce's answer was changed, but the template doesn't re-render. What's the good way to do this?
http://meteorpad.com/pad/KoH5Qu7Fg3osMQ79e/Classification
It's Meteor 1.2 with iron:router added:
<head>
<title>test</title>
</head>
<template name="question">
{{#unless isAnswered 'human'}} <!-- :-< I'm not reacting here -->
<div>Sir, are you classified as human?</div>
<button id="no">No, I am a meat popsicle</button>
<button id="smokeYou">Smoke you</button>
{{else}}
<div> Classified as human? <b>{{answers.human}}</b></div>
{{/unless}}
</template>
And the JavaScript:
// Why isn't this reactive?
if (Meteor.isClient) {
Template.question.helpers({
isAnswered: function (question) { // :-< I'm not reactive
var suspect = Template.instance().data;
return (typeof suspect.answers[question] !== 'undefined');
}
});
Template.question.events({
'click #no': function () {
this.answers.human = "No"; // :-< I'm not reactive
console.log(this);
},
'click #smokeYou': function() {
this.answers.human = "Ouch"; // :-< I'm not reactive
console.log(this);
}
});
}
// Collection
Suspects = new Meteor.Collection('suspects');
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
Suspects.upsert('bruce', { quest: 'for some elements', answers: {}});
});
Meteor.publish('suspects', function() {
return Suspects.find({});
});
}
// Iron Router
Router.route('/', {
template: 'question',
waitOn: function() {
return Meteor.subscribe('suspects');
},
data: function() {
return Suspects.findOne('bruce');
}
});
Thanks :-)
The events are not actually updating the reactive data source (the db record). Instead of doing:
Template.question.events({
'click #no': function () {
this.answers.human = "No";
}
});
The event needs to perform a database action, either through a direct update or through a Meteor.call() to a Meteor.method. For example:
'click #no': function(){
Suspects.update('bruce', {'answers': {'human': 'no'}});
}
If you use this pattern, you will also need to set the correct allow and deny rules to permit the update from client code. http://docs.meteor.com/#/full/allow. Methods generally end up being a better pattern for bigger projects.
Also, I'm not sure off the top of my head that Template.instance().data in your helper is going to be reactive. I would use Template.currentData() instead just to be sure. http://docs.meteor.com/#/full/template_currentdata
Very close you just need to use ReactiveVar by the sound of it it pretty much explains what it's :) http://docs.meteor.com/#/full/reactivevar
and here's how to use it
if (Meteor.isClient) {
Template.question.onCreated(function () {
this.human = new ReactiveVar();
});
Template.question.helpers({
isAnswered: function (question) {
return Template.instance().human.get();
}
});
Template.question.events({
'click #no': function (e, t) {
t.human.set('No');
console.log(t.human.get());
},
'click #smokeYou': function(e, t) {
t.human.set('Ouch');
console.log(t.human.get());
}
});
}
UPDATE: if you're using a cursor I usually like to keep it on the template level not on iron router:
if (Meteor.isClient) {
Template.question.helpers({
isAnswered: function (question) {
return Suspects.findOne('bruce');
}
});
Template.question.events({
'click #no': function (e, t) {
Suspects.update({_id: ''}, {$set: {human: 'No'}});
},
'click #smokeYou': function(e, t) {
Suspects.update({_id: ''}, {$set: {human: 'Ouch'}});
}
});
}

Meteor: data passed to template from Iron Router, is empty at first load

I'm facing a strange problem.
I'm using Iron Router controller to pass data to template:
Router.route('/wards/add/:_id?', {name: 'wards.add', controller: 'WardAddController'});
WardAddController = RouteController.extend({
action: function() {
this.render('addWard', {
data: function(){
return { hospitals : Hospitals.find({}), hospital_id : this.params._id }
}
});
}
});
I return a variable 'hospitals', that should contain all the collection data.
Template:
<div class="jumbotron">
{{#each hospitals}}
{{name}}<br>
{{/each}}
</div>
At first page load, if I type the directly the url of the page, there are no items.
If I type Hospitals.find({}).fetch() (insecure is active) in the browser console, it return an empty object.
But if i change pages, navigating on the website a while, and return the the listing page, items appears.
Any idea?
In the server folder, add publish.js and inside it add:
Meteor.publish('hospitals', function() {
return Hospitals.find({});
});
Then try subscribing to hospitals from your controller:
WardAddController = RouteController.extend({
action: function() {
this.render('addWard', {
waitOn: function() {
return [
Meteor.subscribe('hospitals')
];
},
data: function(){
return { hospitals : Hospitals.find({}), hospital_id : this.params._id }
}
});
}
});

Meteor - router doesn't show layoutTemplate

I have a very simple function that checks wether user is logged in and else send him to the signup page.
var requireLogin = function() {
if (! Meteor.user()) {
this.render('signUpForm', {
path: '/signup',
layoutTemplate: 'signup'
});
}
// else if () {
// this.render('listProfiles');
// }
else {
this.next();
}
};
this is the layoutTemplate (as simple as can be I believe) :
<template name="signup">
<div class="signup-page">
<div class="container">
{{> yield}}
</div>
</div>
</template>
I can't use the layoutTemplate with .render?
I am going to assume you are using Iron Router.
As far as I know, you can't pass the layout template into the render function.
The easiest way get around this would be to use Router config:
// router.js
Router.configure({
layoutTemplate: 'signup'
});
Then in your js:
if (! Meteor.user()) {
this.render('signUpForm');
}

Meteor: wainOn (Iron Route) not working

I have this issue that the rendered function is called while the elements are not yet in the DOM. Let me describe my situation. The templates looks like this:
<template name="barfoo">
<ol>
{{#each bars}}
<li item>{{title}}</li>
{{/each}}
</ol>
</template>
And the following javascript code
Template.barfoo.bars = function () {
return Bars.find({});
};
Tempalate.barfoo.rendered = function () {
var bars = Bars.find({}).fetch();
var list = $('[items]');
};
When the rendered function is called bars == list == [].
So to fix this I implemented Iron routes like this
Bars = new Meteor.Collection('bars');
// Routes
Router.configure({
layout: 'layout',
loadingTemplate: 'loading',
notFoundTemplate: 'notFound'
});
Router.map(function () {
this.route('/', {
controller: 'BarsController',
});
});
if (Meteor.isClient) {
App = {
subs: {
bars: Meteor.subscribe('bars'),
...
}
};
MainController = RouteController.extend({
template: 'barfoo',
before: function () { ... },
waitOn: function () {
return [App.subs.bars];
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
Meteor.publish('bars', function () {
return Bars.find({});
});
}
All basics, but when I go now to localhost:3000 I still get in the rendered function empty lists . Any suggestions what do wrong here ?
I assume your are working with the latest version of iron-router. If that's the case you will also need to add loading hook to your router, so:
Router.onBeforeAction('loading');
Also note that before hook is marked as deprecated.

Meteor - Setting the document title

Is there a way to change the <title> element in a Meteor app? Seems templates are only processed in the <body>.
Pretty much anywhere in your client-side JavaScript code:
document.title = "My new title";
You can extend David Wihl's solution:
Deps.autorun(function(){
document.title = Session.get("DocumentTitle");
});
Then You can in any time call:
Session.set("DocumentTitle","New Document Title");
If you use iron:router you can add the package manuelschoebel:ms-seo to handle the title along with meta tags. This is useful for static and dynamic SEO data:
Router.map(function() {
return this.route('blogPost', {
path: '/blog/:slug',
onAfterAction: function() {
var post = this.data().post;
SEO.set({
title: post.title,
meta: {
'description': post.description
},
og: {
'title': post.title,
'description': post.description
}
});
}
});
});
You can create a helper for setting the title (CoffeeScript):
UI.registerHelper "setTitle", ->
title = ""
for i in [0..arguments.length-2]
title += arguments[i]
document.title = title
undefined
or the same in Js:
UI.registerHelper("setTitle", function() {
var title = "";
for (var i = 0; i < arguments.length - 1; ++i) {
title += arguments[i];
}
document.title = title;
});
Then you can use it in complex ways, and it will be reactive:
{{#if book}}
{{setTitle book.title}}
{{else}}
{{setTitle "My books"}}
{{/if}}
I find it more convenient to handle that kind of thing directly in the router with a onBeforeAction:
Router.map(function() {
return this.route('StudioRoot', {
path: '/',
onBeforeAction: function() {
return document.title = "My Awesome Meteor Application";
}
});
});
you can also include in <head> </head> tags which does not reside in a template. try this:
contents of sample.html:
<head>
<title>my title</title>
</head>
<body>
...
</body>
<template name="mytemplate">
...
</template>
What I ended up doing:
in the Meteor.isClient:
Meteor.startup(function() {
Deps.autorun(function() {
document.title = Session.get('documentTitle');
});
});
now that the var is set reactively, go in the router file (if not already done: meteor add iron:router. My router file is both client and server)
Router.route('/', {
name: 'nameOfYourTemplate',
onBeforeAction: function () {
Session.set('documentTitle', 'whateverTitleIWant');
this.next(); //Otherwise I would get no template displayed
}
});
It doesn't matter if you already set a title in the head tag. It will be replaced by this one according to your route.
I had to look for an answer that would work for ui-router. I know that this might not be the answer you were looking for. Since this question was posted about 2 years ago, I figured if someone else was to come here looking for a solution with ui-router, this answer could help them:
myApp.run.js
(function() {
'use strict';
angular
.module('myApp')
.run(run);
run.$inject = ['$rootScope', '$state'];
function run($rootScope, $state) {
$rootScope.$on("$stateChangeSuccess", function(previousRoute, currentRoute){
document.title = 'myApp - ' + currentRoute.data.pageTitle;
});
};
})();
routes.js
(function() {
'use strict';
angular
.module('myApp')
.config(config);
config.$inject =
['$urlRouterProvider', '$stateProvider', '$locationProvider'];
function config($urlRouterProvider, $stateProvider) {
// ...
$stateProvider
.state('home', {
url: '/',
templateUrl: 'client/home/views/home.ng.html',
controller: 'HomeController',
data: {
pageTitle: 'My Dynamic title'
}
})
}
})();

Resources