by nogridbag · 11y ago · 0 comments
I disagree it's not identical to React+Flux at all. There's more pieces and wiring required for the typical "Save" button example. In react+flux, the views need to listen on stores:
MyStore.listen(this._onMyStoreChange.bind(this));
The views manually call action creators: MyActions.saveData();
Action creators dispatch separate events for beforeRequest, onRequest, onError events: this.actions.onBeforeSaveSomething();
// do the save
this.dispatch({ type: 'SUCCESS', data: myData });
Stores need to listen for action creator events: this.bindAction(actions.onBeforeSaveSomething, this.onBeforeSaveSomething);
this.bindAction(actions.onSave, this.onSave);
this.bindAction(actions.onError, this.onError);
In the Flux flavor I'm using (alt), stores automatically dispatch events when their state is changed, but I found managing the store state is annoying because of the loading, error flags[1].So the store.onSave might look like this:
this.loading = false;
this.error = null;
this.data = data;
Finally, the view updates it's state in response to a store change which automatically calls render.Then in the view render, you can show your loading/saving mask.
There's no Ext magic in the code I posted. Flux and MVC are different patterns. In MVC, the controller typically has direct access to the view and model which is why the code is simple:
myView.setLoading(true);
myModel.save(...)
That would look the same in Java Swing for example. Flux is a fundamentally different pattern and one that I really haven't seen the need for in the products we're building to justify it's disadvantages. But for some applications, it's probably the right solution. myStore.setLoading(true);
myStore.save(...);
Where the handling is in your action handler for whatever event was raised... This assumes you do your backend data access in the store itself... there are other options.