gpt4 book ai didi

javascript - 当底层对象发生变化时,ListView 不会更新

转载 作者:行者123 更新时间:2023-11-28 02:43:39 27 4
gpt4 key购买 nike

我遇到了 ListView 的问题不显示其数据源中对象的最新详细信息。数据源是通过调用 createSorted method 创建的WinJS.Binding.List 对象的。每个对象看起来像这样:

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

我像这样创建/设置数据源:

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

ListView 的 itemTemplate 如下所示:

<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 不会更新以显示新标题。我做错了什么?

最佳答案

当在其基础数据源上调用notifyMutated 时,ListView 似乎没有显式地重新绑定(bind)其元素。如果调用notifyMutated导致元素被移动,那么它将被反弹,因为元素被销毁并重新创建。否则,您需要导致重新绑定(bind)发生。我的更改处理程序现在如下所示:

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();
};

关于javascript - 当底层对象发生变化时,ListView 不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12257318/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com