How receive link attributes of event in Backbone?

陌路散爱 提交于 2019-12-13 00:45:47

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/13807243/how-receive-link-attributes-of-event-in-backbone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!