Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic computed properties in Ember.JS deprecated?

Tags:

ember.js

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?

like image 814
andy Avatar asked Feb 11 '13 12:02

andy


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?

Introduction to JavaScript Computed PropertyThe property name is derived from the value of the propName variable. When you access c property of the rank object, JavaScript evaluates propName and returns the property's value. Like an object literal, you can use computed properties for getters and setters of a class.


2 Answers

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
like image 199
DF_ Avatar answered Nov 08 '22 04:11

DF_


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(),
});
like image 2
rart Avatar answered Nov 08 '22 06:11

rart