goatslacker / alt

Isomorphic flux implementation
http://alt.js.org/
3.45k stars 322 forks source link

How to update state after successful request with sources? #641

Closed ghost closed 8 years ago

ghost commented 8 years ago

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?

WoLfulus commented 8 years ago

I don't know how you are making the request, but, this is how I do it:

class UsersActions {
    /// ... 
    remove(id) {
        axios.post("/users/remove", { id: id }).then((response) => {
            this.actions.removeSuccess(response.data.id);
        }, (error) => {
            this.actions.removeFailed(error);
        });
        this.dispatch();
    }
    /// ... 
}

/// ... 

class RulesStore {
    /// ... 
    constructor() {
        this.users = {};
    }
    /// ... 
    handleRemoveSuccess(rules) {
        if (id in this.users) {
            delete this.users[id];
        }
    }
    /// ... 
}

I use the id in the response in this example, but jusr forwarding the (parameter) id from the remove function works fine too since the scope is saved.

ghost commented 8 years ago

This solved my problem: http://stackoverflow.com/questions/36647928/how-to-update-state-after-successful-request-with-sources-in-alt-js/36741881#36741881