1

モデルのカスタム イベントを作成しようとしていますが、「匿名」関数定義をコールバックとして使用しない限り、どうやらカスタム イベントがトリガーされるようです。

これが私のアプリ構造の擬似コードです

//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
4

2 に答える 2

4

ここで関数を呼び出しています:

this.triggerMe()

それはthis.triggerMeでなければなりません

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

() または .call() または .apply() を追加すると、参照ではなく関数が呼び出されます。

于 2012-09-26T02:38:13.017 に答える
1

渡すthis.triggerMe()ことで、関数が自動的に実行されtriggerMeます (括弧を追加して呼び出すため)。

あなたがする必要があるのは、関数への参照を渡すことです。そのようです:

this.model.on("custom:event", this.triggerMe, this)
于 2012-09-26T02:38:23.133 に答える