[SOLVED] Template argument value differs between helper and event when I have more then one instance of the template


#1

In my effort to make the code example easy to read i stripped away the part that caused the problem. I’m using a label for the input field and therefore the input field has an id and thats of course not ok when repeating the template.

I now use the layoutArea-helper as an id value and every thing works just fine.

Thanks to @robfallows for helping me with this.

--------------------- Original question below-------------------------

I want to include a Blaze template with an argument and then use the argument value in an event. The problem is that when I include the template a second time with a different argument i get the argument value from the first instance of the template in events.

Template:

<template name="UploadFormLayoutImage">
    <form class="uploadPanel">
        <input type="file" name="fileupload" id="input-field">
        <label for="input-field">Upload file</label>
    </form>
</template>

Include:

{> UploadFormLayoutImage layoutArea="area1"}}
{> UploadFormLayoutImage layoutArea="area2"}}

js:

Template.UploadFormLayoutImage.onCreated(function(){
   this.currentArea = new ReactiveVar;
   this.currentArea.set(this.data.layoutArea);
});


Template.UploadFormLayoutImage.helpers({
    layoutArea: function() {
        return Template.instance().currentArea.get(); //Returns the correct argument value for each instance of the template.
    }
});


Template.UploadFormLayoutImage.events({
    'change input[type="file"]': function(e, instance) {
        e.preventDefault();
        console.log(instance.data.layoutArea); //Allways returns 'area1'
    }
});

What am I missing here?


#2

I just copied/pasted your code and it works as expected (logs “area1” or “area2” depending on which button is clicked).

What version of Meteor are you running?


#3

I’m running version 1.4.1.1


#4

Me too. Can you post a repo onto github?


#5

The fact that it’s working for you gives me hope. I will do some additional testing at my end and after that post a repo. Thanks for your help som far.