Coder Social home page Coder Social logo

Where to clean-up stores about flummox HOT 3 CLOSED

acdlite avatar acdlite commented on July 28, 2024
Where to clean-up stores

from flummox.

Comments (3)

acdlite avatar acdlite commented on July 28, 2024

If I understand correctly, you want to clear the stores whenever a new query is submitted?

Sounds like it could be a good use case for the async action handling API we discussed in #14

class MyStore extends Store {
  constructor(flux) {
    super();
    let actions = flux.getActions('myActions');
    this.registerAsync(actions.search, this.handleSearchBegin, this.handleSearchSuccess, this.handleSearchFailure); 
  } 

  handleSearchBegin() {
    // Clear results
    this.setState({
      results: Immutable.List()
    });
  }

  handleSearchSuccess(results) {
    this.setState({ results }); 
  }

  handleSearchFailure(error) {
    // handle error somehow
  }
}

(Note that this feature isn't implemented yet... I'll start working on it sometime today)

Also, you should never set store state using assignment (this.state.data = ...). Always use setState(), as in React. This ensures that the change event is fired properly.

from flummox.

vesparny avatar vesparny commented on July 28, 2024

Yes it's exactly what I meant.
I'd like to find a way to restore the store state while querying for new results.

Your solution looks good, otherwise the only solution would be to fire an action to clean the store, before fetching new data.

Honestly I don't think we have other solution without polluting the one way data flow.

from flummox.

darekrossman avatar darekrossman commented on July 28, 2024

Not sure if this applies in your particular scenario, but I've started using this pattern (from the flummox demo) in my handler views:

statics: {
    async routerWillRun(state, flux) {
      let itemActionIds = flux.getActions('item');
      return itemActions.getItems();
    }
  }

And the main snippet:

Router.run(routes, function (Handler, state) {
  async function run() {
    await performRouteHandlerStaticMethod(state.routes, 'routerWillRun', state, flux);
    React.withContext(
      { flux },
      () => React.render(<Handler />, document.getElementById('app'))
    );
  }
  run().catch(error => {
    throw error;
  });
}); 

And the performRouteHandlerStaticMethod function:

async function performRouteHandlerStaticMethod(routes, methodName, ...args) {
  return Promise.all(routes
    .map(route => route.handler[methodName])
    .filter(method => typeof method === 'function')
    .map(method => method(...args))
  );
}

Coming from Angular, this is essentially the same as resolving some data before instantiating the next controller. In some cases I use this to make sure the new data has arrived before rendering the next route's view. In other cases, when I don't want the delay or I'm not actually changing routes, I have had to implement an additional action to explicitly empty certain objects in my state.

I'm pretty new to Flux in general so I may be telling you what you already know.

from flummox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.