1

我想在此链接的主干视图中接收属性。我不确定这是在 Backbone 中执行此操作的正确方法。也许这个链接的参数应该在视图渲染上设置?

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

我绑定事件的主干代码:

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

1 回答 1

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'); 
}
于 2012-12-10T18:56:12.667 回答