Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember computed properties in Coffeescript

I want to implement the following Javascript code in Coffeescript

App.ItemView = Ember.View.extend({
    classNameBindings: ['itemId'],
    itemId: function() {
        console.log(this.get('content'));
        return "item-%@".fmt(this.get('content.id'));
    }.property('content.id'),
    templateName: 'item'    
}); 

Here is what I have so far in coffeescript:

App.ItemView = Ember.View.extend(
    classNameBindings: ['itemId']

    itemId: ->
        console.log this.get('content')
        contentId = this.get('content.id')
        "item-#{contentId}");
    .property('content.id')

    templateName: 'item'    
)

I get:

Error: Parse error on line 11: Unexpected '.'

The issue seems to be with the dot in .property('content.id') . I dont know how this translates into Coffeescript. How can I properly implement this view in Coffeescript?

like image 433
Aras Avatar asked Oct 08 '12 08:10

Aras


People also ask

What are computed properties in Ember?

What are Computed Properties? In a nutshell, computed properties let you declare functions as properties. You create one by defining a computed property as a function, which Ember will automatically call when you ask for the property. You can then use it the same way you would any normal, static property.

What is computed property in JavaScript?

The computed property names feature was introduced in ECMAScript 2015(ES6) that allows you to dynamically compute the names of the object properties in JavaScript object literal notation. A JavaScript object is just a collection of key-value pairs called properties.

How do I use CoffeeScript in HTML?

If you are looking to implement coffee script in html, take a look at this. You simple need to add a <script type="text/coffeescript" src="app. coffee"></script> to execute coffee script code in an HTML file.


2 Answers

It's beeing a quite long time, but I think this should be written like this:

App.ItemView = Ember.View.extend(
  classNameBindings: ['itemId']

  itemId: (->
    console.log this.get('content')
    contentId = this.get('content.id')
    "item-#{contentId}");
  ).property('content.id')

  templateName: 'item'    
)
like image 134
sly7_7 Avatar answered Oct 11 '22 14:10

sly7_7


itemId: (->
  content = @get 'content'
  if content
    return 'item-%@'.fmt(content.get 'id')
  null
).property('content.id')

You have to protect computed properties from values that might not be defined yet. That is, your code is fine if there's already an id property on the content object. If content is undefined, then you're not going to be able to look up its ID property and you'll probably see a complaint.

You can also use

itemId: Ember.computed(->
  ..
).property('content.id')

and a similar pattern for observers. In fact, an observer would also accomplish the same thing without the conditional:

itemId: null

contentIdChanged: (->
  @set 'itemId', 'item-%@'.fmt(@get 'content.id')
).observes('content.id')
like image 45
tborg Avatar answered Oct 11 '22 15:10

tborg