Dynamic computed properties in Ember.JS deprecated?

早过忘川 提交于 2020-01-22 12:22:25

问题


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?


回答1:


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



回答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(),
});


来源:https://stackoverflow.com/questions/14811838/dynamic-computed-properties-in-ember-js-deprecated

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!