调用erb模板中另一个文件形式中定义的CoffeeScript方法



难道我不能从erb模板中调用这个CoffeeScript方法吗?它不起作用,但似乎应该起作用。

setup.js.coffee

class SetupStepTwo
  include @
  constructor: ->
    @resetView()
  resetView : ->
    console.log('cool');
window.ns1.SetupStepTwo = SetupStepTwo
$ ->
  new SetupStepTwo()

更新.js.erb

window.ns1.SetupStepTwo.resetView();

您的SetupStepTwo类有一个名为resetView实例方法,但当您这样说时,您正试图将其称为的方法(或者至少在(Java|Coffee)Script中称为类方法的方法):

window.ns1.SetupStepTwo.resetView();

如果您真的想使用resetView作为类方法,那么您的类应该更像这样:

class SetupStepTwo
  constructor: ->
    @constructor.resetView()
  @resetView : ->
    console.log('cool')

@resetView上的@构成了一个类方法,而@constructor或多或少与Ruby中的self.class相似。

演示:http://jsfiddle.net/ambiguous/eDdmd/

如果你想让resetView成为一个实例方法,那么你需要替换这个:

window.ns1.SetupStepTwo.resetView();

其中CCD_ 9调用CCD_。

最新更新