Flux体系结构 - 如何在控制器中了解操作中的异步事件何时完成

时间:2021-12-02 19:31:38

I'm learning flux architecture. I'm trying to add new user. When user clicks save button on UI, in my controller I call an action creator's createUser function to initiate async request:

我正在学习通量架构。我正在尝试添加新用户。当用户单击UI上的保存按钮时,在我的控制器中,我调用动作创建者的createUser函数来启动异步请求:

    addNewUser: function () {
        HdfsUsersActions.createUser($scope.newUser);
    }

Action creator function:

动作创建者功能:

    createUser: function (user) {
        return restService.createHdfsUser(user).then(function (data) {
            flux.dispatch('platform-manager-ADD_USER', data);
        });
    }

Then I need to know when async request is over and its status to hide user add UI form in html. What's the correct approach here?

然后我需要知道异步请求何时结束,以及隐藏用户在html中添加UI表单的状态。这里的正确方法是什么?

I can do it like this:

我可以这样做:

HdfsUsersActions.createUser($scope.newUser).then(function() {
   //hide user add form on UI
});

But this doesn't seem to be the right approach because it seems that under the flux architecture the only way a controller gets data is from a store. Here, using then seems to break that rule as the data comes from an action creator

但这似乎不是正确的方法,因为似乎在通量架构下控制器获取数据的唯一方法是来自商店。在这里,使用then似乎打破了该规则,因为数据来自动作创建者

1 个解决方案

#1


0  

When you receive answer from async call, you will dispatch event platform-manager-ADD_USER:

当您从异步调用收到答案时,您将调度event platform-manager-ADD_USER:

flux.dispatch('platform-manager-ADD_USER', data);

Then, you should listen to this event in store, and update itself with received data. Then all components which listen to this change will receive updates and rerender themselves if it's needed.

然后,您应该在商店中收听此事件,并使用收到的数据更新自己。然后,所有听取此更改的组件都将收到更新,并在需要时自行重新呈现。

#1


0  

When you receive answer from async call, you will dispatch event platform-manager-ADD_USER:

当您从异步调用收到答案时,您将调度event platform-manager-ADD_USER:

flux.dispatch('platform-manager-ADD_USER', data);

Then, you should listen to this event in store, and update itself with received data. Then all components which listen to this change will receive updates and rerender themselves if it's needed.

然后,您应该在商店中收听此事件,并使用收到的数据更新自己。然后,所有听取此更改的组件都将收到更新,并在需要时自行重新呈现。