2013-02-11 54 views
9

我正在尝试制作一个应用程序。我有一个计算的属性和控制器看起来是这样的:Ember.JS中的动态计算属性已弃用?

// 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 

现在我下面的教程使用Ember.JS的旧版本。我每天固定的错误,但这样的:

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

什么是另一种方式来做到这一点?

回答

10

计算属性仅在对象的create()函数上被弃用。如果你想创建一个计算属性,那么你首先必须extend()的对象,然后create()它。

例如:

// 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 
+0

哦,我知道了,谢谢。有没有可能有一个例子使用上面发布的代码?此刻,我有点偏离自己的深度。 – andy 2013-02-11 12:31:15

+0

当然,我用代码更新了我的帖子。 – Deif 2013-02-11 12:35:08

+0

非常感谢一堆! – andy 2013-02-12 09:56:32

2

这似乎也工作正常,如果你做了重新打开:

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

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

这实际上是如果你想在你的'应用程序对象。 – Nico 2014-09-02 15:30:50