难道我不能从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_。