Passing a parameter from the template


#1

Hi,
I need to pass the “username” to a function but I can’t find a way to do so…

I Have this template:

<template name="adminPage" >
<table border="1" align="center">
        <tr><th>User</th><th>ElevatorID</th><th>Description</th><th></th><th></th></tr>
        {{#each allusers}}
            <tr><td id="user"><h6>{{username}}</h6></td>
                <td><button id="deleteUser" class="delete btn btn-danger btn-xs" >Delete User</button></td>
            </tr>
        {{/each}}

</table>

and this this client side script:

Template.adminPage.events({
'click .delete': function(e) {            
            console.log(allUsers.username);
                    }
})

any ideas?
tnx!


#2

Do console.log(this) instead and you’ll find the correct variable


#3

Holy Cow! I had been doing this for the last two months:

//template
<div id="wrapper">
<class="btn" data-id='...'>

//js
'#wrapper .btn': function (e) {
    var a = $(e.target).data('id');
}

and no one called me out on it! Man, you do not know how excited this makes me. >_<!


#4

And as a bonus if you call that function with (e, tmp) than u will have access also to template data context as tmp.data
At least I think :smiley:


#5

:open_mouth: :open_mouth: :open_mouth: :open_mouth: :open_mouth: :open_mouth: :open_mouth: :open_mouth: :open_mouth: :open_mouth: :open_mouth: :open_mouth: :open_mouth: :open_mouth: :open_mouth: :open_mouth: :open_mouth: :open_mouth: :open_mouth: :open_mouth:


#6

Yeah naaa, I’m wayyy ahead on this one:


#7

nice!

gonna try it out


#8

Could the contents of this in that context be accessed through Template.currentData or Template.parentData?

I was trying to do something similar to what @xtrim was trying, using Template.currentData, but it hasn’t been working out so far.


#9

I tried this, but got results that differed from what I was expecting:

Inside a call to Template.postCreate.events(function(evt, tpl) { ... }) I put:

  • console.log(this)
  • console.log(tpl)

The output of this was: Object {}

And the output of the tpl argument was a Blaze.TemplateInstance object, but it’s data k/v pair was empty.

I guess my question is, (well, two):

  • Why was the template instance’s data object empty? There were definitely variables in the Spacebars template (e.g. {{name}} )
  • If the this object isn’t a templaate instance in the context of the Event Map (I thought it would be), what is it?

Thanks!


#10

The template’s prop is not the same as this passed into events. There is no relationship I think.
The this passed into event functions refers to the context of any +each or +with object wrapping it. e.g

//template
.btn(onclick="gotoPost")
//js
function (e, t) {
    // this refers to Object {} because there is no context outside of this event
}

// vs

+each postList
    .btn(onclick="gotoPost")
//js
function (e, t) {
    // this has all the props of each postList as it iterates so if your postList is a db.collection.cursor you can write
    this._id // <-- post._id
}

// this does the same thing as +each as in it gives the function call the context of post
+with post
    .btn(onclick="gotoPost")