Where to call server api #1
Comments
Good question, thank you. Calls to the server should only be put in action dispatchers, one [almost] never should make server calls from visual elements. Validation feature in model view is intended for client side validation. For persisting the data and server validation error I would recommend the following approach:
|
So the visual element calls the Model validation and if it passes, emits SAVE_USER. A This is a very similar workflow to what I am doing but I'm using the Polymer listeners to listen for the custom events fired. In one scenario the I'm currently handling that using the Polymer listeners in a parent page that embeds the I'm guessing that translates to putting a |
All action dispatchers exist at the application level. It's up to the developer to logically associate action dispatchers with views, UniFlow doesn't prescribe one specific way. What we do in our project is setting extra options for actions; that gives you various options. Some action dispatchers may always process certain actions, some are conditional. For instance, in your scenario you may have |
Thanks. I just installed and am going to be testing out things today. Uni-flow is more straightforward and "Polymer-ish" than my existing method. |
I have a current solution based on optimizely/nuclear-js and heavily borrowed from home-assistant/home-assistant-js. It is similar in that there is a stateBehavior (the only behavior). Properties are defined with a bindState value that is a nuclear-js getter (function that returns the specified state). A simple example is
It is notified of changes and actions dispatched are used to update the state, just like uniflow. When saving a form we call an action which returns a promise. We can then wait for the response from the server and only leave the screen if successful, showing validation errors if necessary.
The MyApp.userActions.saveUser calls the backend server API to actually send the data and return any additional validation errors such as a non unique username error. It also updates other state such as the errorMessage to display on this screen. If the server returns an error, a Javascript error is thrown and the 'user-save-clicked' event is not fired. On success it is fired and an upstream component handles routing changes.
Where in uniflow should the calls to the server be put? I really like the ModelView validation method but where would it integrate with sending the data to the server and handling errors returned from the server? How would you wait to make sure it was successful before routing to a new screen?
I've reviewed the ToDoMVC app but of course it is only updating local state and not integrating with a server backend.
The text was updated successfully, but these errors were encountered: