Backbone.js Backbonejs和自定义模型事件

Backbone.js Backbonejs和自定义模型事件,backbone.js,Backbone.js,我正试图为我的模型创建一个自定义事件,但显然不管发生什么,自定义事件都会被触发,除非我使用“匿名”函数定义作为回调 下面是我的应用程序结构的伪代码 //Router initialize: -> this.user = new User() this.view = new View({model:this.user}) this.view.render() //View initialize: -> //This event binding get triggere

我正试图为我的模型创建一个自定义事件,但显然不管发生什么,自定义事件都会被触发,除非我使用“匿名”函数定义作为回调

下面是我的应用程序结构的伪代码

//Router
initialize: ->
  this.user = new User()
  this.view = new View({model:this.user})
  this.view.render()

//View
initialize: ->
  //This event binding get triggered no matter what
  //this.model.on("custom:event", this.triggerMe(), this) 

  //This works properly. Only triggered when I call model.trigger("custom:event")
  this.model.on("custom:event", function(){console.log("I WORK!!");}))

triggerMe: ->
  //I GET TRIGGER NO MATTER WHAT

您正在此处调用一个函数:

这是triggerMe()

应该是这个,特里格尔梅

this.model.on("custom:event", this.triggerMe, this)
this.model.on("custom:event", this.triggerMe, this)

添加()或.call()或.apply()调用的是一个函数,而不是对它的引用。

您在这里调用的是一个函数:

这是triggerMe()

应该是这个,特里格尔梅

this.model.on("custom:event", this.triggerMe, this)
this.model.on("custom:event", this.triggerMe, this)

Adding()或.call()或.apply()调用的函数不是对它的引用。

通过传递
this.triggerMe()
您将自动执行
triggerMe
函数(因为您添加了圆括号,并对其进行了调用)

您需要做的是向函数传递一个引用。像这样:


通过传递
this.triggerMe()
您将自动执行
triggerMe
函数(因为您添加了括号,并通过这样的方式对其进行了调用)

您需要做的是向函数传递一个引用。像这样: