简体   繁体   English

回流如何监听异步动作完成

[英]Reflux how to listen for async action completed

From reading the docs I don't quite understand how Reflux async actions work. 从阅读文档我不太了解Reflux异步操作如何工作。 In particular I need to trigger something when an async action completes. 特别是我需要在异步操作完成时触发某些操作。

In one of my components I want to listen for an async action complete and then transition to a view. 在我的一个组件中,我想要监听异步操作完成,然后转换到视图。

mixins: [State, Navigation, Reflux.listenerMixin],

componentDidMount() {
  this.listenTo(actions.loadProject.completed, ()=>{this.transitionTo('info')});
},

I have created my action like this: 我创建了这样的动作:

var actions = Reflux.createActions([
  "someSyncAction",
  "anotherSyncAction"
]);

actions.loadProject = Reflux.createAction({
  "loadProject": {children: ['completed','failed']},
});

And in my projectStore I have something like this: 在我的projectStore中我有这样的东西:

 onLoadProject(id) {

    var url = '/api/projects/' + id;
    io.socket.get(url, (body, jwr) => {

      if(jwr.statusCode !== 200){
        console.error('Failed to load project', id, body);
        return actions.loadProject.failed();
      }

      var p = body;
      debug('loaded project', id, p);
      this.project = p;
      this.trigger(p);
      actions.loadProject.completed();
    });
  },

But it appears actions.loadProject.completed is not a function, so the above code won't work. 但是看起来actions.loadProject.completed不是一个函数,所以上面的代码不起作用。 What is the correct approach? 什么是正确的方法?

I found that my original code didn't work because of one typo and one mistake. 我发现我的原始代码因为一个错字和一个错误而无效。 Below are the corrections. 以下是更正。

mixins: [State, Navigation, Reflux.listenerMixin],

Should have been 本来应该

mixins: [State, Navigation, Reflux.ListenerMixin],

I believe warnings for undefined mixins have been added to React, but apparently didn't make it into my version yet. 我相信未定义的mixins的警告已添加到React中,但显然还没有进入我的版本。

actions.loadProject = Reflux.createAction({
 "loadProject": {children: ['completed','failed']},
});

Should have been 本来应该

actions.loadProject = Reflux.createAction({children: ['completed','failed']});

I had used the syntax from createAction s instead. 我曾使用过createAction 语法。 That's why loadProject.completed wasn't a function. 这就是loadProject.completed不是函数的原因。 Reflux created a plain action without complaining apparently. 反射创造了一个简单的行动,没有显然抱怨。

In Tim Arney's example shows that you can keep the API call in a separate action listener and have the store only listen for the completed action. 在Tim Arney的示例中,您可以将API调用保留在单独的动作侦听器中,并使存储仅侦听已完成的操作。 I think I prefer to keep the API call with the store logic. 我想我更喜欢使用商店逻辑保持API调用。 If anyone thinks there's a good reason not to I'd love to hear about it. 如果有人认为我有充分的理由不喜欢听到它。

I'm new to Reflux myself, here's a demo I put together. 我自己是Reflux的新手,这是我放在一起的演示。 Not sure if it's 100% proper but might help you - http://jsbin.com/roqito/2/edit 不确定它是否100%正确但可能对您有帮助 - http://jsbin.com/roqito/2/edit

From docs : 来自docs

// Create async action with `completed` & `failed` children 
var makeRequest = Reflux.createAction({ asyncResult: true });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM