0

我有一個todos列表,我有與他們相關的標籤屬性。在視圖上,我有一個「按標籤排序」按鈕。當我按下該按鈕時,我希望集合按標籤排序。現在什麼都沒有發生。以下是代碼。怎麼了?SproutCore收集排序

In todos.js I have: 
Todos.SortingView = SC.TemplateView.extend({ 
    sortBinding: 'Todos.todoListController.sortTodos' 
}); 

and in todoListController, I have: 
    sortTodos: function() { 
    Todos.store.find(Todos.Todo).sortProperty('tag'); 
    } 

and in the handlebars view I have: 
{{#view Todos.SortingView id="stats"}} 
    {{#view SC.Button classBinding="isActive" target="Todos.todoListController" action="sortTodos"}} 
    Sort By Tag 
    {{/view}} 
{{/view}} 

{{#collection SC.TemplateCollectionView contentBinding="Todos.todoListController" itemClassBinding="content.isDone"}} 
    {{view Todos.MarkDoneView}} - Tag - {{content.tag}} 
{{/collection}} 
+0

什麼是您的ListController? – hvgotcodes

+0

Todos.todoListController = SC.ArrayController.create({ //使用一個空數組初始化陣列控制器 內容:[], //功能 sortTodos:函數(){ Todos.store.find(託多斯.Todo).sortProperty( '標籤');} , //功能 }); –

回答

1
sortTodos: function() { 
    Todos.store.find(Todos.Todo).sortProperty('tag'); 
} 

應該是:

sortTodos: function() { 
    this.set('orderBy', 'tag'); 
} 

現在,如果你添加項目,必須使用addObject並使用arrangedObjects不僅僅是content


createTodo: function(title) { 
    // var todo = Todos.Todo.create({ title: title });  
    var todoExists = this.findProperty('title', title).length > 0; 
    if (!todoExist) { 
     Todos.store.createRecord(Todos.Todo, { title: title }); 
     // this.pushObject(todo); 
    } 
} 
+0

非常好,非常感謝。我還有一個問題,我添加一個新的項目,它增加了一個收藏。所以,我在視圖中有兩次相同的待辦事項。 –

+0

我在視圖中做錯了什麼或者是什麼?這是我的整個車把模板: –

+0

Todos

{{#view Todos.CreateTodoView}} {{/視圖}} {{#view Todos.SortingView ID = 「統計」}} {{#view SC.Button classBinding = 「isActive」 目標= 「Todos.todoListController」 行動=「sortTodos 「}} 排序標籤 {{/視圖}} {{/視圖}} {{#collection SC.TemplateCollectionView contentBinding =」 Todos.todoListController「}} {{視圖Todos.MarkDoneView}} - 標籤 - {{content.tag}} {{/ collection}} –

0

在todoL istController,假設您最初從數據存儲中加載控制器的內容。

sortTodos: function(){ 
var query= SC.Query.local('Todos.Todo', {orderBy: 'tag'}); 
Todos.todoListController.set('content',Todos.store.find(query)); 
}