我是Flux
和React
的早期和非常快乐的采用者,所以最近,我将Fluxxor移植到Android中,到目前为止一直没问题。
我遇到的问题Flux正在处理单个项目或详细信息页面的数据。请耐心等待我。我会尽量保持清醒。
我正在使用的模式是。
在页面加载(componentWillMount
/ componentWillReceiveProps
和onStart
)上,我检查传递给页面的ID(通过url或bundle)是否与当前项目的id相匹配如果商店处于处理或成功状态,则存储和页面。
如果是,我什么也不做,否则,我会发送一个动作来加载该项目的数据。
componentWillMount: function () {
id = this.props.params.path.split("-")[0];
var artistData = this.props.state.artistData;
if(artistData.id != id)
this.getFlux().actions.artistActions.loadArtist(id);
else if (!artistData.artist && !artistData.loading)
this.getFlux().actions.artistActions.loadArtist(id);
this.getFlux().actions.userActions.fetchSuggestions();
}
protected void onStart() {
GenreSongsStore.State state = App.getFlux().getStore(GenreSongsStore.class).getState();
if(mId == state.Genre.getId()) {
if (state.HasMore)
App.getFlux().getActions().Genres.songs(mId, state.Page + 1);
}
else
App.getFlux().getActions().Genres.songs(mId, 1);
super.onStart();
}
在React中,这很好,因为您在根上使用单个状态。在我开始使用Android之前,我没有太多麻烦。 在这里,我不使用单一的状态,但查询相关的商店,它完全闻起来
但是,我得到了维持当前加载状态的好处。因此,用户可以最小化并重新打开应用程序,我们继续(无需保存实例包)。
我知道通过这样做,我失去了unidirectional data flow
的好处。但它似乎在Android(双关语)的背景下更有意义。
我能否就你如何做到这一点以及如果我只是担心什么都没有意见。
注意:数据不会被其他任何商店共享。