jquery $(this) 不适用于 CoffeeScript 和 Backbone

发布于 2024-12-17 19:11:18 字数 1199 浏览 1 评论 0原文

$(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感性 2024-12-24 19:11:18

在主干文档中,http://backbonejs.org/#View-delegateEvents
它明确指出:

所有附加的回调在传递之前都绑定到视图
到 jQuery,所以当调用回调时,this 继续引用
到视图对象。

这解决了许多面向对象程序员在使用 jQuery 时遇到的问题之一,即它会覆盖所有事件处理程序中的上下文。如前所述,您经常可以使用事件,该事件作为处理程序的参数传递。但是,event.target 通常可以引用子元素,而不是 this,后者始终引用通过查询选择的元素。例如:

<a href="#"><b>my link</b></a>

$('a').on('click', myfunction); function myfunction(event) {
    this // refers to the "a" element
    event.target // refers to the "b" element 
}

在这些情况下,当使用 Backbone 将鼠标事件附加到具有子元素的元素时,请使用 event.currentTarget 来获取对事件处理程序附加到的元素的引用。

events: { 
  "click a" : "myFunction" }, 
...
function myFunction(event) {
  var $this = $(event.currentTarget);
  // Now, $this is the same as jQuery's $(this)
}

In the backbone documentation, http://backbonejs.org/#View-delegateEvents
It states clearly:

All attached callbacks are bound to the view before being handed off
to jQuery, so when the callbacks are invoked, this continues to refer
to the view object.

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 to this, which always refers to the element selected with the query. For instance:

<a href="#"><b>my link</b></a>

$('a').on('click', myfunction); function myfunction(event) {
    this // refers to the "a" element
    event.target // refers to the "b" element 
}

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.

events: { 
  "click a" : "myFunction" }, 
...
function myFunction(event) {
  var $this = $(event.currentTarget);
  // Now, $this is the same as jQuery's $(this)
}
超可爱的懒熊 2024-12-24 19:11:18

这一切都取决于上下文,

this 在某些情况下绑定到您的视图本身,
举个例子:

var myView = Backbone.View.extend({
    events: { "click a" : "myfunction" },
    initialize: function(){
        _.bindAll(this, 'render', 'myfunction');
    },
    render: function(){
        // rendering data here...
    },
    myfunction: function(e){
        console.log(e.target); // will log the clicked DOM element => <a>
        console.log(this);  // will log the view => myView
    }

});

window.v = new myView({ el : $('#mydiv') });
window.v.render();

正如你所看到的,如果你应该运行这个例子,你可以看到 thise.target 之间的区别。然而,这都是由于初始化方法中的关键线造成的。

_.bindAll(this, 'methodname', [ methodnames ] );

当使用给定方法之一时,this 将视图绑定到 this
如果您要从该列表中删除 myfunctionconsole.log(this); 将记录该元素本身。
但是,您将无法从您的视图中访问数据或函数......
您可以自由选择是否将视图绑定到 this

it all depends on the context,

this is in certain cases bound to your view itself,
take this example:

var myView = Backbone.View.extend({
    events: { "click a" : "myfunction" },
    initialize: function(){
        _.bindAll(this, 'render', 'myfunction');
    },
    render: function(){
        // rendering data here...
    },
    myfunction: function(e){
        console.log(e.target); // will log the clicked DOM element => <a>
        console.log(this);  // will log the view => myView
    }

});

window.v = new myView({ el : $('#mydiv') });
window.v.render();

as you can see, if you should run this example, you can see the differences between this and e.target. however, this is all due to the keyline in the initialize method.

_.bindAll(this, 'methodname', [ methodnames ] );

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.

卷耳 2024-12-24 19:11:18

对于通过 Backbone 视图绑定的事件,this 将设置为视图本身。这与普通 jQuery 不同,其中 this 引用 DOM 元素

,因此在 Backbone 视图中:

events: { "click a" : "myfunction" },
...
function myfunction(e) {
    this // refers to the view
    e.target // refers to the element
}

而在普通 jQuery 中:

$('a').on('click', myfunction);
...
function myfunction() {
    this // refers to the element
    e.target // also refers to the element
}

在这两种情况下,您都可以使用 e.target 来引用到元素

With events bound through a Backbone view, this will be set to the view itself. This is different from normal jQuery where this refers to the DOM element

So in a Backbone view:

events: { "click a" : "myfunction" },
...
function myfunction(e) {
    this // refers to the view
    e.target // refers to the element
}

Whereas in normal jQuery:

$('a').on('click', myfunction);
...
function myfunction() {
    this // refers to the element
    e.target // also refers to the element
}

In both cases, you can use e.target to refer to the element

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文