我正在尝试制作一个余烬应用程序。我有一个计算属性,控制器看起来像这样:
// 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.
这样做的替代方法是什么?
答案 0 :(得分: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
答案 1 :(得分:2)
如果重新打开它似乎也可以正常工作:
Todos.todosController = Ember.Controller.create({
// ** SNIP ** //
});
Todos.todosController.reopen({
countCompleted: function() {
return this.get('todos').filterProperty('completed', true).length
}.property(),
});