简体   繁体   English

Backbone .on事件侦听器,带有带有coffeescript的回调函数

[英]Backbone .on event listener with callback function with coffeescript

How can I correctly write this in coffee script? 如何正确地在咖啡脚本中编写此内容?

this.model.on('change:game', function(){
  this.render();
}, this)

I am trying everything I can think of, and I'm currently on this: 我正在尝试我能想到的一切,而我目前正在这样做:

@model.on 'change:game', -> 
  @render()
@

I am confused how to handle multiple arguments when one of them is an anonymous function, the first 2 parameters have a comma, but if I try to add a comma before the 3rd parameter I get syntax errors. 当它们中的一个是匿名函数时,我很困惑如何处理多个参数,前两个参数带有逗号,但是如果尝试在第三个参数之前添加逗号,则会出现语法错误。

I believe you can do something like this: 我相信你可以做这样的事情:

@model.on "change:game", => @render()

With CoffeeScript, you can leverage the "fat" arrows to lexically bind this and simplify your code. 使用CoffeeScript,您可以利用“胖”箭头以词法绑定this绑定并简化您的代码。

You don't actually need an anonymous function. 您实际上不需要匿名函数。 When you say: 当你说:

model.on(event, fn, context)

in JavaScript, the event handling system will end up saying: 在JavaScript中,事件处理系统最终会说:

fn.apply(context, whatever_arguments_are_used)

The fn can by any function at all, it doesn't have to be anonymous. fn完全可以具有任何功能,而不必是匿名的。

Moving to CoffeeScript and remembering that @render is a reference to the render function, we can say: 转到CoffeeScript并记住@render是对render函数的引用,我们可以说:

@model.on 'change:game', @render, @

and skip the anonymous function altogether. 并完全跳过匿名功能。 Alternatively, you could use listenTo to automatically supply the desired @ : 另外,您可以使用listenTo自动提供所需的@

@listenTo @model, 'change:game', @render

This sort of structure can be a problem if the fn above takes arguments that don't match what the event system will pass. 如果上面的fn接受的参数与事件系统将传递的参数不匹配,则这种结构可能会成为问题。 In those cases, you can use a bound anonymous function as wmock suggests , the multiline approach you found, or some parentheses: 在这种情况下,您可以使用wmock建议绑定匿名函数 ,发现的多行方法或一些括号:

@model.on 'change:game', (-> @render()), @
#                        ^            ^ 

or listenTo again: 或再次listenTo

@listenTo @model, 'change:game', -> @render()

You have four ways: 您有四种方法:

  1. simple 简单

     @model.on 'change:game', -> @render() , @ 
  2. fat arrow 胖箭头

     @model.on 'change:game', => @render() 
  3. without anonymous function 没有匿名功能

     @model.on 'change:game', @render, @ 
  4. with parenthesis 带括号

     @model.on 'change:game', (-> @render()), @ 

And Your second question about how to handle multiple parameters, when some of them are anonymous function. 还有您的第二个问题,当其中一些是匿名函数时,如何处理多个参数。

Four examples, that explain everything: 四个例子可以解释一切:

param, func 参数,功能

func param1, () ->
  # anonymous function 1
  return

param, func, param 参数,功能,参数

func param1, () ->
  # anonymous function 1
  return
, param2

param, func, func 参数,功能,功能

func param1, () ->
  # anonymous function 1
  return
, () ->
  # anonymous function 2
  return

param, func, param, func 参数,函数,参数,函数

func param1, () ->
  # anonymous function 1
  return
, param2, () ->
  # anonymous function 2
  return

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

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