如何在Backbone中接收事件的链接属性?

时间:2012-12-10 18:50:29

标签: backbone.js backbone-events

我想在此链接的Backbone View中接收属性。我不确定这是在Backbone中做到这一点的正确方法。也许应该在视图渲染上设置此链接的参数?

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

我的Backbone代码绑定一个事件:

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'); 
}

1 个答案:

答案 0 :(得分:7)

您需要将事件传递给deletePost并通过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'); 
}