在Ember,Ember.SortableMixin sortProperties中排序对象无法按预期工作

时间:2015-05-22 18:00:50

标签: javascript ember.js todomvc

我是Ember的新手,最近遵循了Ember“入门”指南来构建TodoMVC。这是jsbin

一切正常,但现在我试图通过向TodosController添加todos来对sortProperties进行排序:

Todos.TodosController = Ember.ArrayController.extend({
    sortProperties: ['title'],
    sortAscending: true,
    ...
});

在模板中,我有这个:

{{#each todo in arrangedContent itemController="todo"}}
  ...
{{/each}}

基于Ember.SortableMixin documentation,我所做的似乎是合理的,但显然我错过了一些东西。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

感谢stevennunez,我明白了缺少的内容 - 添加以下代码解决了问题:

Todos.TodosIndexController = Ember.ArrayController.extend({
    sortProperties: ['title'],
    sortAscending: true
});

如果没有定义此控制器,默认情况下,Ember会创建一个"泛型" Ember.ArrayController用于呈现todos/index模板。因此,放置在sortProperties中的TodosController无效。