2015-01-14 14 views
2

在我的应用程序中,我做了两个Ajax调用以获取一块数据。首先,我拨打电话获取ecommerceIntegrations的列表。一旦我有了,我可以抓住他们各自的orders嵌套通量数据

我当前的代码看起来是这样的:

componentDidMount: function() { 
    EcommerceIntegrationStore.addChangeListener(this._onIntegrationStoreChange); 
    OrderStore.addChangeListener(this._onOrderStoreChange); 

    WebshipEcommerceIntegrationActionCreators.getEcommerceIntegrations(); 
}, 

_onIntegrationStoreChange: function() { 
    var ecommerceIntegrations = EcommerceIntegrationStore.getEcommerceIntegrations(); 
    this.setState({ecommerceIntegrations: ecommerceIntegrations}); 
    ecommerceIntegrations.forEach(function(integration) { 
     WebshipOrderActionCreators.getPendingOrdersOfIntegration(integration.id); 
    }); 
}, 

_onOrderStoreChange: function() { 
    this.setState({ 
     pendingOrders: OrderStore.getAllPendingOrders(), 
     pendingOrdersByIntegration: OrderStore.getPendingOrdersByIntegration() 
    }); 
} 

我试图按照Facebook的流量模式,我敢肯定,这并不遵循它。我看到一些关于Flux嵌套数据的其他SO帖子,但我仍然不明白。任何指针赞赏。这里

回答

3

一切看起来都不错,除了这一点:

ecommerceIntegrations.forEach(function(integration) { 
    WebshipOrderActionCreators.getPendingOrdersOfIntegration(integration.id); 
}); 

而不是试图把火关在回答另一个动作的动作(或更糟糕的是,在ecommerceIntegrations每一个项目的动作),备份和回应原来的行动。如果您还没有完整的数据集,并且您需要对服务器进行两次调用,请等待激发该操作,直到获得完整更新系统所需的所有数据。在XHR成功处理程序中关闭第二个调用,而不是在视图组件中。这样,您的XHR调用与调度周期无关,并且您已将应用程序逻辑移出视图并进入更适合封装的区域。

如果您确实想要在第一次调用后更新应用程序,则可以在进行第二次调用之前在XHR成功处理程序中调度一个操作。

理想情况下,您可以在一次调用中处理所有这些事情,但我明白,如果Web API不在您的控制范围内,则有时不可能这样做。

在Flux应用程序中,不应将Actions视为可作为严格事件序列链接在一​​起的事物。他们应该彼此独立生活,如果您有连锁的冲动,您可能需要备份并重新设计应用程序对原始操作的响应方式。