Fred J. February 2016

Set default value if nothing is selected

The code below is expected to set headerLabel to "Select an Item" when the app first starts. Then once an item is selected, the headerLabel is expected to show the item name. I am able to get it to do the second parts but not the first. Thanks

//---main_menu.js-----------------------
Template.mainMenu.helpers({
  menuItems: [
    {menuItem: "task1"},
    {menuItem: "task2"},
    {menuItem: "task3"},
    {menuItem: "task4"},
    {menuItem: "task5"},
    {menuItem: "task6"},
    {menuItem: "task7"}
    ]
});

Template.mainMenu.events({
  'click .menuItem': function(event){
    Session.set('selectedItem', $(event.currentTarget).data('value'));
  }
});

//---header.js-----------------------
Template.header.events({
  'click .mainMenu': function(){
    alert("show the main menu ");
  }
});

Template.header.helpers({
  headerLabel: function(){
    var selected = Session.get('selectedItem');
    if (selected === "") {
      headerLabel: "Select an item";
    } else {
      return selected;
    }
  }
});
//---main_menu.html--------------------
<template name="mainMenu">
  <div class="container">
    <div class="row">
      <section class="col-xs-12">
        <div class="list-group">
          {{#each menuItems}}
            <a href="#" class="list-group-item menuItem" data-value={{menuItem}}>
              <img src="/abc.png">
              {{menuItem}} <span class="badge">&#x3e;</span>
            </a>
          {{/each}}
        </div>
      </section>
    </div>s
  </div>
</template>

//---header.html-----------------------
<template name="header">
  <h1>
    <button class        

Answers


durrrr February 2016

Very simple actually:

<h3><label class="col-xs-8 text-center">{{#if headerLabel}}{{headerLabel}}{{else}}Default value{{/if}}</label></h3>

And in js code you can just do

Template.header.helpers({
  headerLabel: function(){
    return Session.get('selectedItem');
  }
});

Post Status

Asked in February 2016
Viewed 2,986 times
Voted 13
Answered 1 times

Search




Leave an answer