2012-09-04 95 views
1

我在ListView的數據源中沒有顯示對象的最新詳細信息時遇到問題。 dataSource通過調用WinJS.Binding.List對象的createSorted method來創建。每個對象看起來是這樣的:當底層對象發生變化時,ListView不會更新

var obj = { 
    title: 'First item', 
    priority: 2 
}; 

我創建/設置DataSource這樣的:

sortedItemList = itemList.createSorted(function (lhs, rhs) { 
    return rhs.priority - lhs.priority; 
}); 
listView.itemDataSource = sortedItemList.dataSource; 

ItemTemplate中爲ListView看起來像這樣:

<div id="itemTemplate" data-win-control="WinJS.Binding.Template"> 
    <div> 
     <h4 data-win-bind="innerText: title"></h4> 
    </div> 
</div> 

的變化處理程序兩個字段都是這樣的:

titleControl.onchange = function() { 
    curItem.title = titleControl.value; 
    sortedItemList.notifyMutated(sortedItemList.indexOf(curItem);); 
}; 
priorityControl.onchange = function() { 
    curItem.priority = priorityControl.value; 
    sortedItemList.notifyMutated(sortedItemList.indexOf(curItem);); 
}; 

createSorted的文檔說,確保每當對象更改時調用notifyMutated。如果我改變優先級,那麼ListView將適當地移動項目。但是如果我編輯標題,則ListView不會更新以顯示新標題。我究竟做錯了什麼?

回答

1

當notifyMutated調用其基礎數據源時,ListView不顯式重新綁定其元素。如果對notifyMutated的調用導致元素被移動,那麼它將被反彈,因爲元素被銷燬並被重新創建。否則,您需要重新綁定。我的更改處理程序現在看起來像這樣:

var notifyMutated = function() { 
    var prevIndex, 
     postIndex; 

    prevIndex = sortedItemList.indexOf(curItem); 
    sortedItemList.notifyMutated(prevIndex); 
    postIndex = sortedItemList.indexOf(curItem); 

    if (postIndex !== prevIndex) { 
     WinJS.Binding.processAll(listView.elementFromIndex(postIndex), curItem); 
    } 
}; 

titleControl.onchange = function() { 
    curItem.title = titleControl.value; 
    notifyMutated(); 
}; 
priorityControl.onchange = function() { 
    curItem.priority = priorityControl.value; 
    notifyMutated(); 
}; 
+0

如果您使用JS模板,該代碼如何工作? – rbyrne

相關問題