简体   繁体   中英

Controller properties displaying as [object Object] (when using coffeescript)

I have a controller written in CoffeeScript as follows:

class App.SidebarController extends Ember.ObjectController
  namez: (->
    @get('name')
  ).property()

This however returns as [object Object] in my view when I call the {{namez}} tag.

I re-wrote the same controller in JavaScript and the property prints out to the template as expected:

App.SidebarController = Ember.ObjectController.extend({
  namez: function() {
    return this.get('name');
  }.property()
});

Any ideas as to why the CoffeeScript version isn't returning the proper value?

The extends keyword of Coffeescript does basic prototype based inheritance. Ember's extends is actually a much more sophisticated object system that provides getters and setters, computed properties among other things. The 2 are not equivalent.

The equivalent Coffeescript for Ember would be,

App.SidebarController = Ember.ObjectController.extend
  namez: (->
    @get('name')
  ).property()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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