Hey - I didn’t mean to criticize the documents, and made my comments above from memory! Having gone back and looked at the opening paragraphs I think my criticism was unjust. Have the dox changed recently?
To be honest, when I go to the dox page now, I just scroll down looking for some configuration detail.
However, I do have a big question which I allude to in my comments.
Using connectTo() only sets up state from bind() onwards. From experience there seems to be no reason why the state cannot be initialized in the ctor, other than the fact that you have to manually setup and teardown the subscription in unbind() or detached().
Would it not be possible to have connectTo() do it’s magic in the ctor instead of bind()?
Also, is there no mechanism to auto register the actions? It’s a lot of extra typing to store.registerAction("action",action)
With state setup in the ctor(), activate() now has access to it. One of the things I’ve tried to do is reduce as much as possible passing parameters through routes to views, much preferring (where possible) to pick up the “params” from the state. Personally I find managing the parameters for routes a bit of a nightmare, because I cannot use a rest backend. I use RavenDb, whose Id is of the format customer/1-A
, which means that I have to send the query to the backend with the parameters encoded as query variables. Not a big deal - I just need to remember to do it!
The other “issue” I have is accessing state within the service. It would be nice if there was some shortcut to doing this. At the moment I set up a subscription for the state, extract the readonly property I’m interested in (effectively a global variable), and then tear down the subscription - all in the same method call.
My biggest complaint about aurelia-store is the same I would make against Aurelia in general - most of the time it so flexible, there are so many different ways of getting to the same final result (note I did not say there are so many different ways to do the same thing), that it can be terribly confusing for a newbie. I’ve been using Aurelia since it first came out, and still most definitely consider myself a newbie. I learn new stuff all the time from comments in the these threads - particularly you and @bigopon and @khuongduybui and @huochunpeng comments.
The biggest initial confusion I had was what I should be doing in the action. I had always used a service to call the back end, and returned the data to the viewModel directly from the service. This of course led to the problem that aurelia-store resolves so well.
At one point, I must have read the dox to mean that I should make the calls to the backend in the action, and spent a long time struggling with this, since it was so contrary to what I had been doing until then. I then read a comment by @khuongduybui in one of the threads that completely opened my eyes - it allowed me to continue to use my services as I was comfortable with - viewModel asks service for data => service asks back end for data : service “massages” the data from the server => service sends action updating the state.
My services are now basically one liners, and the actions do nothing more that clone the state, update the state with the new data, and return the cloned state.
To close, I guess it would be nice for the dox to have some broad overview of the possible “best practices” at the start, before delving into rxjs observables (which I still don’t understand - and frankly, don’t have to understand to use the store).
I would also warn in capital letters NOT TO BIND.TWO-WAY DIRECTLY TO STATE - the problem is you don’t necessarily realize that you are doing it until it’s too late, particularly if you haven’t cloned the state properly {… state}.
As a belts and braces measure, in the viewModel I clone the state that I’ve already cloned in the action again, just to make double sure I’m binding to a truly different object.
Apologies for the ramble …
Jeremy