我有一个包含以下内容的subscriptionObserver:
return update(
previousResult,
{
ApptsForCurrentUser: {
$push: [newAppt],
},
}
);
通过订阅到达的新项目需要按日期排序顺序插入到ApptsForCurrentUser数组中。它是一个多维数组,我可以使用$apply
函数对其进行排序。
在将数组移交给将对其进行排序的$push newAppt
函数之前,数组是否有$apply
的语法?
或者,我应该这样做吗?
(尚未测试):
var newResult = clonedeep(previousResult); //lodash
newResult.push(newAppt);
newResult.sort(myCustomSortFunction);
const newResultAsAConst = clonedeep(newResult);
return update(
previousResult, newResultAsAConst
);
答案 0 :(得分:0)
根据@Sacha的建议,我能够通过以下代码解决这个问题:
import update from 'immutability-helper';
[.....]
const resultWithNewApptAdded = update(previousResult, {getAllApptsForCurrentUser: {$push: [newAppt]}});
//getAllApptsForCurrentUser contains unsorted list of appts
resultWithNewApptAdded.getAllApptsForCurrentUser = resultWithNewApptAdded.getAllApptsForCurrentUser.sort(mySortFunction);
return resultWithNewApptAdded;