【问题标题】:Ember coffeescript @ inside a function on object is window instead of object对象上的函数内的 Ember coffeescript @ 是窗口而不是对象
【发布时间】:2014-09-21 08:40:53
【问题描述】:

我似乎无法在属性上定义的函数内引用父对象。

SocketMixin = Ember.Mixin.create

  thing: (->
    'dougs'
  ).property()

  getThing: ->
    console.log @ # window object
    @get('thing') # Uncaught TypeError: undefined is not a function 

  sameWithFatArrow: =>
    console.log @ # window object
    @get('thing') # Uncaught TypeError: undefined is not a function 

  printThing: ->
    @get('getThing')() # call the above function

计算属性没有问题

  thingProp: (->
    @get('thing')
  ).property()

工作得很好。

我一直试图通过在参数中传递 @ 来解决这个问题,但我很确定肯定有比这更好的方法来调用这些函数。

  getThing: (self) ->
    console.log @ # window object
    self.get('thing') # Uncaught TypeError: undefined is not a function 

  printThing: ->
    @get('getThing')(@) # call the above function

真的,我只想调用一个方法。它应该比所有这些都更加困难!?!?

【问题讨论】:

    标签: ember.js coffeescript scope this


    【解决方案1】:

    您正在窗口范围内调用该函数。不要使用getter,只需调用func。只有在处理属性时才需要获取器和设置器。函数仍以相同的方式调用。

    printThing: ->
        @getThing() # call the above function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2014-04-14
      • 2014-09-12
      • 1970-01-01
      • 2014-08-08
      • 1970-01-01
      相关资源
      最近更新 更多