Himanshu Yadav
Himanshu Yadav

Reputation: 13585

Meteor: Changing the variable value from click event

I just want to show the note for the clicked day.
Here is how my app is structured:
homePage.html

<template name="homePage">
    <h2>Welcome to home page!</h2>
    {{#each posts}}
        {{> postOnDay}}
    {{/each}}
</template>

postOnDay.html

<template name="postOnDay">
    <a href="#" class="aDay">{{day}}</a>
    <div style="display:{{showPost}}">
        {{note}}    
    </div>
</template>

homePage.js

Template.homePage.helpers({
   posts: function() {
        //alert(Posts.find().count());
        return Posts.find();
    }
});

postOnDay.js

Template.postOnDay.events({
    'click .aDay': function(e) {
        e.preventDefault();
        var link = e.currentTarget;
        showPost: 'inline' //change value on click
    }
});

Template.postOnDay.helpers({
    showPost: 'none' //default value
})

Upvotes: 0

Views: 1227

Answers (1)

sites
sites

Reputation: 21815

If you want to change values in a helper and in a event handler you should use Session:

Instead of

     showPost: 'inline' //change value on click
   }
});

Template.postOnDay.helpers({
    showPost: 'none' //default value
})

I would do:

      Session.set('showPost', 'inline'); //change value on click
    }
});

Template.postOnDay.helpers({
    showPost: function(){ return Session.get('showPost')}
})

For a default value, use:

Session.setDefault('showPost', 'inline')

This could be in template or application initialization according your needs.

Another thing to take into account is that if you want a variable per post you might need to use something like:

Session.get('showPost' + this.id)

Be sure to output the value of this in console, so you are sure this.id is unique per post.

Upvotes: 1

Related Questions