简体   繁体   English

尝试通过传入函数绑定回调会引发错误

[英]Trying to bind a callback by passing in the function throws an error

I just want to fire an event when an input changes value using jQuery 1.7.2 and Backbone.js. 我只想在输入使用jQuery 1.7.2和Backbone.js更改值时触发事件。

Currently I have the following (which works) 目前我有以下(有效)

MyView: Backbone.View.extend({
  initialize: function() {

    this.colorInput = $("<input />", {
         "id": "color",
         "name": "color",
         "value": this.model.get("color")
    });

    var self = this;
    this.colorInput.on("change", function() {
      self.changeColor();
    });

  },
  changeColor: function() {
    var color = this.colorInput.val();
    this.model.set("color", color);
  }
});

I was trying to do it the other way where I just pass in my function. 我正试图通过我的功能传递另一种方式。

this.colorInput.on("change", this.changeColor, this);

But when trying to do it that way, it throws the error 但是当试图这样做时,它会抛出错误

((jQuery.event.special[handleObj.origType] || {}).handle || handleObj.handler).apply is not a function ((jQuery.event.special [handleObj.origType] || {})。handle || handleObj.handler).apply不是函数
.apply( matched.elem, args ); .apply(matched.elem,args);

( line 3332 ) 第3332行

Which I'm not able to figure out. 我无法弄明白。 Any ideas why this way doesn't work? 任何想法为什么这种方式不起作用?

You're confusing jQuery's on : 你混淆jQuery的on

.on( events [, selector] [, data], handler(eventObject) ) .on(events [,selector] [,data],handler(eventObject))
.on( events-map [, selector] [, data] ) .on(events-map [,selector] [,data])

with Backbone's on : Backbone on

object.on(event, callback, [context]) object.on(event,callback,[context])

Backbone's takes a context as the third argument, jQuery's doesn't. Backbone将上下文作为第三个参数,jQuery没有。 Looks like jQuery's on is interpreting your third argument as the handler(eventObject) and trying to call it like a function, that would explain the error message that you're seeing. 看起来像jQuery的on是解释第三个参数作为handler(eventObject) ,并试图把它像一个功能,这可以解释你所看到的错误消息。

Normally you'd do it more like this: 通常你会这样做:

MyView: Backbone.View.extend({
  events: {
    'change input': 'changeColor'
  },
  initialize: function() {
    this.colorInput = $("<input />", {
       "id": "color",
       "name": "color",
       "value": this.model.get("color")
    });
  },
  render: function() {
    this.$el.append(this.colorInput);
    return this;
  },
  changeColor: function() {
    var color = this.colorInput.val();
    this.model.set("color", color);
  }
});

and let Backbone's event delegation system take care of things. 让Backbone的事件委托系统处理好事情。

This is for the Googlers who come across this problem. 这适用于遇到此问题的Google员工。 I had this exact same issue and what occurred was that where the error was being reported and where the error actually occurred were in two different places. 我有这个完全相同的问题,发生的事情是报告错误的地方和实际发生的错误位于两个不同的地方。

One line of code was obsolete and looked something like 一行代码已过时,看起来像

$('#modal').on('click', function(e) {
   // Execute invalid code here.
});

The other line of code was similar: 另一行代码类似:

$('#modal').on('click', function(e) {
   // Execute valid code here.
});

The error was that the first invocation was not a true function, so the error was accurate, the second handler being invoked was not a true function and jQuery couldn't complete it, but it always acted as if it occurred on the second function call. 错误是第一次调用不是一个真正的函数,所以错误是准确的,被调用的第二个处理程序不是一个真正的函数,jQuery无法完成它,但它总是表现得好像它发生在第二个函数调用上。

I'd say if you run into this error, remove any extra event handlers that may be triggered and see if that stops the problem. 我会说如果你遇到这个错误,删除任何可能被触发的额外事件处理程序,看看是否能解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM