Gediminas Šukys
Gediminas Šukys

Reputation: 7391

How receive link attributes of event in Backbone?

I want to receive attributes in Backbone View of this link. I'm not sure this is right way to do this in Backbone. Maybe params of this link should be set on view rendering?

<a class="postDeleteLink" data-id="5" data-hash="Hgsda45f">Delete</a>

My Backbone code to bind an event:

PostListView = Backbone.View.extend({
events: {
    "click .postDeleteLink": "deletePost"
},
deletePost: function(){
    //standart jquery way doesn't work, because "this" is already used by backbone
    var id = $(this).attr('data-id'); 
    var hash = $(this).attr('data-hash'); 
}

Upvotes: 1

Views: 1139

Answers (1)

fbynite
fbynite

Reputation: 2661

You need to pass the event to deletePost and access via currentTarget.

PostListView = Backbone.View.extend({
events: {
    "click .postDeleteLink": "deletePost"
},
deletePost: function(e){
    var id = $(e.currentTarget).attr('data-id'); 
    var hash = $(e.currentTarget).attr('data-hash'); 
}

Upvotes: 7

Related Questions