简体   繁体   English

Coffeescript缩进有问题

[英]Trouble with Coffeescript Indent

I'm trying to translate this into Coffeescript: 我正在尝试将其翻译成Coffeescript:

App.IndexView = Ember.View.extend(InfiniteScroll.ViewMixin, {
  didInsertElement: function(){
    this.setupInfiniteScrollListener();
  },
  willDestroyElement: function(){
    this.teardownInfiniteScrollListener();
  }
});

My first approach was this: 我的第一种方法是:

Whistlr.OrganizationsView = Em.View.extend
  InfiniteScroll.ViewMixin
  didInsertElement: ->
    @setupInfiniteScrollListener()
  willDestroyElement: ->
    @teardownInfiniteScrollListener()

But it threw an unexpected indent error. 但是它引发了意外的缩进错误。 So I tried this instead: 所以我尝试这样做:

Whistlr.OrganizationsView = Em.View.extend InfiniteScroll.ViewMixin
  didInsertElement: ->
    @setupInfiniteScrollListener()
  willDestroyElement: ->
    @teardownInfiniteScrollListener()

Which gives me this error: 这给了我这个错误:

TypeError: InfiniteScroll.ViewMixin is not a function

If I use the plain js, it works fine. 如果我使用纯js,效果很好。 So the problem is definitely with the Coffeescript formatting. 因此,问题肯定出在Coffeescript格式上。 I'm having trouble figuring out what's going on, or even how to properly search for an explanation. 我无法弄清楚发生了什么,甚至无法正确地寻找解释。 Any pointers would be appreciated! 任何指针将不胜感激!

Try 尝试

Whistlr.OrganizationsView = Em.View.extend InfiniteScroll.ViewMixin,
  didInsertElement: -> @setupInfiniteScrollListener()
  willDestroyElement: -> @teardownInfiniteScrollListener()

The second parameter of .extend needs to be an JavaScript object. .extend的第二个参数必须是JavaScript对象。

This is a quick translation of the JS to Coffeescript. 这是JS到Coffeescript的快速翻译。 When I paste it into the 'Try Coffeescript' browser window, it produces the same JS (with added returns). 当我将其粘贴到“ Try Coffeescript”浏览器窗口中时,它会生成相同的JS(带有返回值)。 The extra (){} are for my benefit, not CS's. 多余的(){}是出于我的利益,而不是CS的利益。 They make it clear to me that this is a call to extend , with 2 arguments, one an object attribute, the other an object with two function definitions. 他们向我清楚地表明,这是对extend的调用,它带有两个参数,一个是对象属性,另一个是带有两个函数定义的对象。

App.IndexView = Ember.View.extend(
    InfiniteScroll.ViewMixin,
    {
    didInsertElement: ()  ->
        @setupInfiniteScrollListener()
    willDestroyElement: () ->
        @teardownInfiniteScrollListener()
    }
)

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

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