S
S
Stepan2015-11-08 21:22:31
JavaScript
Stepan, 2015-11-08 21:22:31

How to first send an event from the view to the store, work with it there and then trigger it?

How to first send an event from a view to the store, work with it there and then trigger it to reach another view?
It so happens to me that the event is sent (send 1), comes to the store, there I do (1 + 1 = 2). And then it flies to another view and for some reason it comes again 1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
fayster, 2015-11-12
@xoma2

When the number comes to the store, we write the amount to a variable and do an emit, while another view will receive a new value, for example:

class Store extends EventEmitter {
    constructor() {
        super();

        this._dispatchToken = dispatcher.register(this._registerToActions.bind(this));
      
        this._id = 1;
    }

    _registerToActions(action) {
        switch(action.actionType) {
            case GENERATE_NEW_ID:
                this._id += action.id;
                this.emit(GENERATE_NEW_ID);
                break;
            default:
                break;
        }
        return true;
    }

    get id() {
        return this._id;
    }
}

class Listener extends React.Component {
    constructor() {
        super();

        this.go = this.go.bind(this);

        Store.on(GENERATE_NEW_ID, this.go);

        this.state = {
            id: Store.id
        };
    }

    go() {
        this.setState({
            id: Store.id
        })
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question