9

I am trying to make an ember application. I have a computed property and the controller looks like this:

// The Controller

Todos.Controller = Ember.Controller.create({

    // ** SNIP ** //

    countCompleted: function()
    {
        return this.get('todos').filterProperty('completed', true).length
    }.property(),
});

// The View

{{Todos.Controller.countCompleted.property}} Items Left

Now the tutorial I'm following is using an older version of Ember.JS. I've fixed every error but this:

Uncaught Error: assertion failed: Ember.Object.create no longer supports defining computed properties.

What's the alternative way to do this?

aceofspades
  • 7,568
  • 1
  • 35
  • 48
andy
  • 2,369
  • 2
  • 31
  • 50

2 Answers2

10

The computed property is only deprecated on the create() function of an object. If you wish to create a computed property, then you must first extend() the object, and then create() it.

For example:

// The Controller

Todos.TodosController = Ember.Controller.extend({

    // ** SNIP ** //

    countCompleted: function()
    {
        return this.get('todos').filterProperty('completed', true).length
    }.property(),
});

// Note the lower case 't' here. We've made a new object
Todos.todosController = Todos.TodosController.create();

// The View


// We reference the created object here (note the lower case 't' in 'todosController')
{{Todos.todosController .countCompleted.property}} Items Left
DF_
  • 3,743
  • 25
  • 34
  • Oh I see thank you. Would it be possible to have an example using the above posted code? I'm a bit out of my depth at the moment. – andy Feb 11 '13 at 12:31
  • @Deif If I want to add computed properties in Application level, Do I extend the Ember.Application? – NkS Apr 17 '13 at 10:16
  • The Application object is a funky one. You can try it, but the 'standard' way of doing stuff like that is to just extend your own object and create it (i.e. use it as a 'helper' object) after you created the main Em.Application object. In the old days then you could do whatever you want to the Application object, but nowadays I think the dev team are pushing to just leave it alone and let it only handle setting up and logging. – DF_ Apr 18 '13 at 10:19
  • Can you also use createWithMixins? – JuJoDi Oct 22 '13 at 14:42
  • You can but you have to merge the two methods together: `Todos.todosController = Em.Controller.extent(...).createWithMixins();` – DF_ Oct 22 '13 at 19:39
2

It also seems to work ok if you do a reopen:

Todos.todosController = Ember.Controller.create({
    // ** SNIP ** //
});

Todos.todosController.reopen({
    countCompleted: function() {
        return this.get('todos').filterProperty('completed', true).length
    }.property(),
});
rart
  • 21
  • 1
  • 1
    This is actually the way to go if you want computed property on your `Application` object. – Nico Sep 02 '14 at 15:30