【发布时间】:2015-03-15 14:24:58
【问题描述】:
我正在尝试向我的 Flux 模型添加乐观更新。我将 UI 操作分派和服务器操作分派合并为一个操作。我在动作创建器中的代码如下所示:
deleteItem: function(itemId) {
// optimistic update
WebshipDispatcher.handleServerAction({
type: ActionTypes.DELETE_ITEM,
deleteStatus: 'success',
itemId: itemId
});
// now let's actually check if that was the correct result
AppAjaxUtil.get('/deleteItem', {itemId: itemId}, function(result) {
WebshipDispatcher.handleServerAction({
type: ActionTypes.DELETE_ITEM,
deleteStatus: result.status, // 'success' or 'failure'
itemId: itemId
});
}, function(error) {
WebshipDispatcher.handleServerAction({
type: ActionTypes.DELETE_ITEM,
error: error
});
});
}
这是允许乐观更新的适当方式还是我对这篇文章的看法不正确?
【问题讨论】:
-
一般来说,这看起来不错。这里代码的一大缺失部分是商店中发生的事情。如果没有看到该代码,很难说你是否做错了什么。我认为大多数人会为您正在进行的不同操作创建单独的操作类型,但不一定有任何理由这样做——它可能会使商店里的东西更干净一些。
标签: reactjs asynchronous reactjs-flux optimistic-ui