Calling a method on a function definition in coffeescript

前端 未结 4 396
清酒与你
清酒与你 2021-01-17 16:05

How would you translate this snippet of javascript to coffeescript? Specifically I\'m struggling with how to call .property() on the function definition.

相关标签:
4条回答
  • 2021-01-17 16:45

    There are a couple ways to define computed properties. Here are examples of each:

    MyApp.president = Ember.Object.create
      firstName: "Barack"
      lastName: "Obama"
      fullName: (-> 
        @get 'firstName' + ' ' + @get 'lastName'
      ).property('firstName', 'lastName')
    
    MyApp.president = Ember.Object.create
      firstName: "Barack"
      lastName: "Obama"
      fullName: Ember.computed(-> 
        @get 'firstName' + ' ' + @get 'lastName'
      ).property('firstName', 'lastName')
    
    0 讨论(0)
  • 2021-01-17 16:56

    I think this is how you're supposed to write it:

    MyApp.president = SC.Object.create {
      firstName: "Barack",
      lastName: "Obama",
      fullName: (-> 
        return @get 'firstName' + ' ' + @get 'lastName'
        # Call this flag to mark the function as a property
      ).property('firstName', 'lastName')
    }
    

    checkout this link

    0 讨论(0)
  • 2021-01-17 16:57

    When using Ember.computed, you do not need to call .property() so you can use this form as well:

    MyApp.president = Ember.Object.create
      firstName: "Barack"
      lastName: "Obama"
      fullName: Ember.computed -> @get 'firstName' + ' ' + @get 'lastName'
    
    0 讨论(0)
  • 2021-01-17 16:59

    Something like this will work?

     (() => this.get("firstName") * this.get("lastName")).property()
    
    0 讨论(0)
提交回复
热议问题