简体   繁体   中英

loading meteor template on click events

So, Basically i'm new to meteor(0.8.2) and trying to create a basic app having two templates(addnewPlace and Map) and a single button. What i need to get is that, when i click on "Add new Place" button, template "addNewPlace" should be loaded in body or else template "Map" should be loaded. Help will be appreciated :)

My html code:

<body>
    {{> menu}}
    {{> body}}

</body>

<template name="body">
    {{#if isTrue}}
        {{> addnewPlace}}// tested this template individually, it works.
    {{else}}
        {{> maps}} // tested this template individually, it works too.
    {{/if}}
</template>

<template name="menu">
<h1>Bank Innovation Map</h1>
<input type="button"  value="Add new Place">
</template>

My js code:

Template.body.isTrue = true;
Template.menu.events({
    'click input': function(){
        //load a new template
    console.log("You pressed the addNewplace button");//this fn is called properly
        Template.body.isTrue = true;
    }

    });

Well first of all you obviously aren't changing anything in the click event (true before, true after). But also if you did, I think you might be better off using a session variable for this, to maintain reactivity.

Session.setDefault('showAddNewPlace', false)
Template.body.isTrue = function() { Session.get('showAddNewPlace'); }
Template.menu.events({
    'click input': function(){
        //load a new template
        console.log("You pressed the addNewplace button");//this fn is called properly
           Session.set('showAddNewPlace', true)
    }
});

Meteor 0.8.2 comes in with the dynamic template include feature. Just set a session variable value on click event and you would like to use the template name on the event.

Session.setDefault('myTemplate', 'DefaultTemplateName');
"click input": function (event) {
    Session.set("myTemplate", 'template_name');
}

You can now write this:

<body>
    {{> menu}}
    {{> body}}

</body>

<template name="body">
  {{> UI.dynamic template=myTemplate}}
</template>

<template name="menu">
<h1>Bank Innovation Map</h1>
<input type="button"  value="Add new Place">
</template>

You may like to take a look at this article for the reference: https://www.discovermeteor.com/blog/blaze-dynamic-template-includes/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM