The UIModel
class exposes a HTTPClient
property for fetching resources.
It also exposes 4 unimplemented functions (Get, Post, etc..) [I don't see any reason for that in the current implementation]
a common use case is to inherit this class and immediately add 2 bound properties for state tracking.
isBusy
- should be true whenever the model is currently using the HTTPClient - can be use to show a loading spinner in the view.
hasError
- (can hold the error itself instead of a simple boolean) whenever an error occurred while using the HTTPClient - can be used for displaying the view accordingly.
those properties should not be serialized, deserialized, saved, discarded or watched for changes for isDirty
like other properties of the class that inherits from UIModel
.
I think it should be implemented Out-Of-The-Box inside the UIModel
class so we don't need to replicate this in every child class.
I see a couple of ways to do this.
- the child class can implement 4 function
_Get
, _Post
etc..
but the caller of the function calls for Get
, Post
etc..
calling Get
will kick of initialization code, and then run _Get
.
something like:
Get(...rest)
{
this.isBusy = true;
this.hasError = false;
this._Get(rest)
.catch(e => this.hasError = e)
.then(_ => this.isBusy = false);
}
the child class can implement the _Get
function as he wants..
.. do the same for Post, Put, Delete..
- the child implements the 4 function just like he does now.
Get
, Post
etc...
but we expose one additional function in the UIModel
class InvokeHttpWithState
, that receive the function to call as a parameter.
something like:
InvokeHttpWithState(methodName, ...rest)
{
this.isBusy = true;
this.hasError = false;
this[methodName](rest)
.catch(e => this.hasError = e)
.then(_ => this.isBusy = false);
}
what do you think?
I can work on this and create a PR + add examples to the DEMO + add documentation.
(a reminder about the demo app - it's not the same as the code currently in the repo)