Thinking in ReactJS and Flux

About Dispatcher

When new data comes into the dispatcher, it then uses these callbacks to propagate that data to all of the stores

换句话说,所以异步的事情,例如向服务器获取数据,应该是在Dispatch之前完成的,当data flow到store之后,马上就会反映到view上面去。

如果Store之间存在依赖关系

As an application grows, dependencies across different stores are a near certainty. Store A will inevitably need Store B to update itself first, so that Store A can know how to update itself. We need the dispatcher to be able to invoke the callback for Store B, and finish that callback, before moving forward with Store A. To declaratively assert this dependency, a store needs to be able to say to the dispatcher, “I need to wait for Store B to finish processing this action.” The dispatcher provides this functionality through its waitFor() method.

用waitFor来解决

官网这个例子不错 ,主要是说一个国家更新的时候,对应的城市也连带更新,最关键的一段代码

CityStore.dispatchToken = flightDispatcher.register(function(payload) {
  if (payload.actionType === 'country-update') {
    // `CountryStore.country` may not be updated.
    flightDispatcher.waitFor([CountryStore.dispatchToken]);
    // `CountryStore.country` is now guaranteed to be updated.

    // Select the default city for the new country
    CityStore.city = getDefaultCityForCountry(CountryStore.country);
  }
});

注意,尽管这是一个CityStore,但是它还是照样响应的是country-update的事件,并且强调要在CountryStore完成更新之后对自己进行更新

你可能感兴趣的:(Thinking in ReactJS and Flux)