Reputation: 270
I'm passing a data context through Iron Router to the page. I can call it with this._id.
But when you have a click event on element within an each loop via spacebars, it changes 'this' to that object I just clicked. Which totally makes sense.
However, how would I grab some properties from the element I just clicked but also get params info I defined from the router?
For example if I had a list of posts:
{{#each posts}}
<li class="post-title">{{title}}</li>
{{/each}}
And I click those elements listed:
'click .post-title': function(){
console.log(this) //this will be the _id of the post I clicked.
var new_object = {
title: this.title, //from that object I want that title
page_url: // But here I want the params data I defined in the router //
}
}
If it wasn't a click event, I could just say, this._id...but as you can see in this instance I can't. Any help would be appreciated. Thanks.
Upvotes: 1
Views: 369
Reputation: 5254
You can also do it this way:
Say that your page is in client/views/posts/post_item.html and inside it you have:
<div class="post-title">
Something To Click On
</div>
Your JS file for this would be in client/views/posts/post_item.js and inside it:
'click .post-title': function() {
Router.go('postEdit', {_id: this._id});
}
The {_id: this._id}
part is how you pass to Iron Router the _id of the post that you clicked .post-title
in. Iron Router needs things to be passed in as a hash / key-value.
And then in the router.js:
this.route('postEdit', {
path: '/posts/:_id/edit',
data: function() { return Posts.findOne(this.params._id); }
});
Upvotes: 0
Reputation: 3158
You can access the data of the current route like so:
'click .post-title': function(){
var data = Router.current().data();
console.log(data);
}
Upvotes: 2