jquery $(this) 不适用于 CoffeeScript 和 Backbone
$(this) 不适用于 cofeescript 和backbone。它生成有效的代码,但我不明白为什么它不起作用。为了解决这个问题,我使用了 $(event.target)。
Jobmatch.Views.Jobs ||= {}
class Jobmatch.Views.Jobs.JobView extends Backbone.View
template: JST["recommendation/templates/jobs/job"]
initialize: () ->
@ajs = new Jobmatch.Collections.ApplicantJobsCollection()
@ajs.reset(@options.applicant_jobs || [])
@aj = new @ajs.model()
@index = @options.index || 0
events:
"click .job_apply" : "apply"
tagName: "tr"
apply: (event)->
target = $(this) // As this is not working as I expected,So I used below line.
target = $(event.target)
if @options.user_jobmatch_valid
@ajs.create({job_id: @model.get('id') })
target.parents("a.job_apply").prev().click();
else
target.parents("a.job_apply").next().click();
false
render: ->
$(this.el).html(@template(@model.toJSON()))
@
这个 cofeescript 生成了以下代码:
JobView.prototype.apply = function(event) {
var target; target = $(this); // not working it is not elementt object
target = $(event.target);// this is element object ,working fine
target.parents("a.job_apply").prev().click();
};
$(this) is not working with cofeescript and backbone.Its generating valid code but I don't understand why it was not working. For work around I used $(event.target).
Jobmatch.Views.Jobs ||= {}
class Jobmatch.Views.Jobs.JobView extends Backbone.View
template: JST["recommendation/templates/jobs/job"]
initialize: () ->
@ajs = new Jobmatch.Collections.ApplicantJobsCollection()
@ajs.reset(@options.applicant_jobs || [])
@aj = new @ajs.model()
@index = @options.index || 0
events:
"click .job_apply" : "apply"
tagName: "tr"
apply: (event)->
target = $(this) // As this is not working as I expected,So I used below line.
target = $(event.target)
if @options.user_jobmatch_valid
@ajs.create({job_id: @model.get('id') })
target.parents("a.job_apply").prev().click();
else
target.parents("a.job_apply").next().click();
false
render: ->
$(this.el).html(@template(@model.toJSON()))
@
And this cofeescript has generated following code:
JobView.prototype.apply = function(event) {
var target; target = $(this); // not working it is not elementt object
target = $(event.target);// this is element object ,working fine
target.parents("a.job_apply").prev().click();
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在主干文档中,http://backbonejs.org/#View-delegateEvents
它明确指出:
这解决了许多面向对象程序员在使用 jQuery 时遇到的问题之一,即它会覆盖所有事件处理程序中的上下文。如前所述,您经常可以使用事件,该事件作为处理程序的参数传递。但是,
event.target
通常可以引用子元素,而不是this
,后者始终引用通过查询选择的元素。例如:在这些情况下,当使用 Backbone 将鼠标事件附加到具有子元素的元素时,请使用 event.currentTarget 来获取对事件处理程序附加到的元素的引用。
In the backbone documentation, http://backbonejs.org/#View-delegateEvents
It states clearly:
This resolves one of the issues that many object-oriented programmers have with jQuery, that it overrides the context in all its event handlers. As stated earlier you can often use the event, which is passed as the handler's argument. However,
event.target
can often refer to child elements as opposed tothis
, which always refers to the element selected with the query. For instance:In these cases, when attaching mouse events with Backbone to elements with children, use
event.currentTarget
to get a reference to the element that the event handler was attached to.这一切都取决于上下文,
this
在某些情况下绑定到您的视图本身,举个例子:
正如你所看到的,如果你应该运行这个例子,你可以看到
this
和e.target
之间的区别。然而,这都是由于初始化方法中的关键线造成的。当使用给定方法之一时,this 将视图绑定到
this
。如果您要从该列表中删除
myfunction
,console.log(this);
将记录该元素本身。但是,您将无法从您的视图中访问数据或函数......
您可以自由选择是否将视图绑定到
this
。it all depends on the context,
this
is in certain cases bound to your view itself,take this example:
as you can see, if you should run this example, you can see the differences between
this
ande.target
. however, this is all due to the keyline in the initialize method.this binds the view to
this
when in 1 of the given methods.if you would remove
myfunction
from that list,console.log(this);
would log the element itself.but, you would not be able to reach data or functions from your view....
you are free to chose if you bind the view in to
this
.对于通过 Backbone 视图绑定的事件,
this
将设置为视图本身。这与普通 jQuery 不同,其中this
引用 DOM 元素,因此在 Backbone 视图中:
而在普通 jQuery 中:
在这两种情况下,您都可以使用
e.target
来引用到元素With events bound through a Backbone view,
this
will be set to the view itself. This is different from normal jQuery wherethis
refers to the DOM elementSo in a Backbone view:
Whereas in normal jQuery:
In both cases, you can use
e.target
to refer to the element