Search code examples
javascriptfluxalt.js

How to update state after successful request with sources in alt.js?


I am fairly new to alt.js and have a question regarding updating the state in the success handler.

Let's say I have a UserStore and want to delete one User. I fire the delete user action with the id of the user which is ought to be deleted. The source sends a delete request with that id to the backend. The request works and the sources fire the success action.

How do I remove the User from the StoreState in the success action handler, since I don't get the id or any other information about the made request passed?


Solution

  • You can use interceptResponse function

    So, basically, you should have some construction like this in your DataSource:

    deleteUser: {
       remote (state, id) {
          //Some backend call here
       },
       interceptResponse (data, action, args) {
          // Here you can access arguments, that were passed to the remote method 
          // via 'args' argument. What you return from this function will be passed
          // to further callbacks (success, error, etc.)
    
          return {data: data, id: args[0]};
       }
    }
    

    So in this case, with the data, returned by remote call, you will also pass the original id to the callbacks and will be able to delete the user from your store.