如何在骨干接收事件的链接属性? [英] How receive link attributes of event in Backbone?

查看:149
本文介绍了如何在骨干接收事件的链接属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要接受这个环节的骨干视图属性。我不知道这是在骨干网做这个正确的方式。也许这个链接的PARAMS应该在视图渲染设置?

<一类=postDeleteLink数据ID =5数据的哈希=Hgsda45f>删除< / A>

我的骨干code绑定的事件:

  PostListView = Backbone.View.extend({
事件:{
    点击.postDeleteLink:deletePost
},
deletePost:功能(){
    //非标准jQuery的方式不起作用,因为这已经由骨干
    VAR ID = $(本).attr(数据ID);
    VAR哈希= $(本).attr('数据的哈希');
}


解决方案

您需要通过传递currentTarget当前的事件deletePost和访问。

  PostListView = Backbone.View.extend({
事件:{
    点击.postDeleteLink:deletePost
},
deletePost:功能(E){
    VAR ID = $(e.currentTarget).attr(数据ID);
    VAR哈希= $(e.currentTarget).attr('数据的哈希');
}

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

解决方案

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

这篇关于如何在骨干接收事件的链接属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆