jeudi 11 février 2016

Flux: Detail Page/Single Item Store

I'm an early and very happy adopter of both Flux and React so much so that, recently, I ported Fluxxor into Android and it's been okay so far.

The issue I am having with it Flux is dealing with data for a Single Item or Details Page. Bear with me below. I will try to be as clear as I can.

The pattern I am using is.

On page load(componentWillMount/componentWillReceiveProps and onStart), I check if the id passed to the page (via url or bundle) matches the id of the item currently in the store and the page if the store is in a processing or success state. If yes, I do nothing, else, I dispatch an action to load the data for that item.

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();
}

In React this is fine since you use a single state on the root. I didn't bother too much until I started working with Android. Here, I don't use a single state but query the relevant store and it totally smells

  1. If you are not using that page, the data is still held in memory
  2. Since the data is not shared it seems there is no benefit to doing it like this
  3. Won't it simply be easier to load the data in the component/activity/fragment?

However, I get the benefit of maintaining the currently loading state. So the user can minimize and reopen the app and we continue (no need for saving an instance bundle).

I know by doing it like this, I lose the benefit of unidirectional data flow. But it seems to make more sense in the context of Android (pun intended).

Can I have your views on how you do this and if I'm simply worried about nothing.

NB: The data is not shared by any other stores at.

Aucun commentaire:

Enregistrer un commentaire