Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor, How do I change a template (view) on event?

I'm building an app with two views: home & view list

When the user clicks on a list's name on the home view, it should change to the "view list" template. I've added a session variable called 'view', setting it to 'home' on startup. When a click event is detected on one of the items (list name) on the home screen, it changes the value of view to 'viewList'. Then in the HTML, I have an if statement to show the home template if 'view' is 'home', otherwise show the 'viewList' template.

I can tell the first part works because I'm outputting the value of 'view' and it does output the value "viewList" when you click on a list name, it just doesn't change the template.

What am I missing?

My code:

mylists.js:

Lists = new Mongo.Collection("lists");

if (Meteor.isClient) {
  Meteor.startup( function() {
    Session.set("view", "home");
  });

  Template.main.helpers({
    view: function () {
      return Session.get("view");
    }
  });

  Template.home.helpers({
    lists: function () {
      return Lists.find({}, {sort: {lastUsed: -1}});
    }
  });

  Template.home.events({
    "submit #new-list": function (event) {
      var name = event.target.listName.value;
      Lists.insert ({
        name: name,
        createdAt: new Date(),
        lastUsed: new Date()
      });
    },
    "click .list-row": function (event) {
        Session.set("view", "viewList");
    }
  });
}

mylists.html:

<head>
  <title>My Lists</title>
</head>

<body>
    {{> main}}
</body>

<template name="main">
    {{view}}
    {{#if view "home"}}
        {{> home}}
    {{else}}
        {{> viewList}}
    {{/if}} 
</template>

<template name="home">
  <header>
    <h2>My Lists</h2>
  </header>
    <ul id="lists">
        <li>
            <form id="new-list">
                <input type="text" name="listName" value="My List 1">
                <input type="submit" value="Save">
            </form>
        </li>
        {{#each lists}}
            {{> list}}
        {{/each}}
    </ul>
</template>

<template name="viewList">
  <header>
    <h2>View List</h2>
  </header>
<!-- list details will show here -->
</template>

<template name="list">
    <li class="list-row" id="{{_id}}">{{name}}</li>
</template>
like image 357
Josh Avatar asked Feb 16 '15 06:02

Josh


1 Answers

If you want to change from templates view i suggest you tu install the iron:router package.

run

meteor add iron:router

lester create the routes.js on the /lib folder

now lets do this step by step.

First create 1 template on myAppName/client/views/layout.html

<template name="layout">
   {{> yield}}
</template>

and update the routes.js with this code.

Router.configure({
    layoutTemplate: 'layout' // here we say that layout template will be our main layout
});

Now on the same routes.js create this 2 routes.

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

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

With this if you navigate to localhost:3000/home or /viewList you will see the html content on there.

NOTES: <header> inside the templates you don't need it.

Now this is just an example because i don't really know whats your main idea here.

you are calling {{#each lists}} inside the home template, so whats the point of having the viewList template?

Now if you want to create individual and dynamic routes for each list you can try this.

Router.map(function () {
  this.route('listViews', {
    path: '/listViews/:_id',
    waitOn: function(){
        return Meteor.subscribe('lists')
    },
    data: function(){
      return Lists.findOne({_id: this.params._id});
    }
  });
}); 

Now with this, you have dynamic templates for each object on the List collection, if you go to localhost:300/listView/35dwq358ew for example you will get render the listView with some data.

you can do stuff like this inside the template list

<template name="list">
    <li class="list-row" id="{{_id}}">{{name}}</li>
    <li><a href="/listViews/{{this._id}}">Check this List in detail</a></li>
</template>

and the viewList template will look like this.

<template name="viewList">
    <h2>{{title}}</h2>
<!-- list details will show here -->
    {{informationAboutList}}
</template>
like image 98
Ethaan Avatar answered Nov 01 '22 10:11

Ethaan