For example, there's a View Model called PokemonTableViewModel which is used to populate a TableView with Pokemon data from the backend. It uses RxSwift for the input and outputs. There's an input function (well, a PublishRelay) that will trigger getting the data, and three output events:
Showing an activity indicator while getting the data
Load the data when the data is retrieved
Show an error message when the data retrieval failed
My questions are:
Which one is better to name the input?
Using the name of the action (eg. PokemonTableViewModel.getData)
Using the name of the View Controller's lifecycle method (eg.
PokemonTableViewModel.onViewDidAppear)
Which one is better to name the outputs?
Using the name of the actions that should be done by the View (eg. PokemonTableViewModel.showActivityIndicator, PokemonTableViewModel.populateTable, PokemonTableViewModel.showErrorMessage)
Using the name of the actions that triggered the events (eg. PokemonTableViewModel.onDataLoading, PokemonTableViewModel.onDataRetrieved, PokemonTableViewModel.onDataFailed)
Thanks.
Related
I need a good example of using a service singleton as data source for my Angular 2 application.
Scenario is as following:
I have an application that is loading prices of some items from the local database (in my case MongoDB).
A few of the components need to use a service which will be the universal source of truth for item prices throughout the application. These prices can be acted upon externally: user can change currency, so they have to be recalculated, or can change the date range for which price averages will be calculated.
So I need to have a singleton service which will load upon app initialization and components need to load prices only after the service data store has been initialized with prices. Also, components need to refresh data(I guess using Observable pattern) when, say, the currency or date range has been changed. Perhaps the best way is to inject the service in the app component, so it gets initialized first?
Is there a recipe or proposed architecture for this kind of app?
I can't call some init function from each component with ngOnInit() because I want data available in multiple components. I need to know in each component when to initialize it with data from Service's data store. I need to know when data is ready.
The way I did it in Angular 1.x is to instantiate the service, and in constructor initialize data, and then when the data is initialized, emit a $rootScope event to tell all components that data is ready.
I can't find a proper recipe to do the same thing in Angular 2.
You need to create a service and define it when bootstrapping your application:
bootstrap(App, [ SingletonService ]);
This way you will have a single instance for the whole application.
If you want to initialize things, you can use it constructor. To notify other elements that use the service, you can use one or several properties of EventEmitter. This way you will be able to emit events when data are there or when something changes. Components could subscribe on these EventEmitters to be notify...
We are currently using visjs version 3 to map the dependencies of our custom built workflow engine. This has been WONDERFUL because it helps us to visualize the flow and find invalid or missing dependencies. What we want to do next is simplify the process of building the dependencies using the visjs manipulation feature. The idea would be that we would display a large group of nodes and allow the user to order them correctly. We then want to be able to submit that json structure back to the server for processing.
Would this be possible?
Yes, this is possible.
Vis.js dispatches various events that relate to user interactions with graph (e.g. manipulations, or position changes) for which you can add handlers that modify or store the data on change. If you use DataSets to store nodes and edges in your network, you can always use the DataSets' get() function to retrieve all elements in you handler in JSON format. Then in your handler, just use an ajax request to transmit the JSON to your server to store the entire graph in your DB or by saving the JSON as a file.
The oppposite for loading the graph: simply query the JSON from your server and inject it into the node and edge DataSets' using the set method.
You can also store the networks current options using the network's getOptions method, which returns all applied options as json.
I'm using Simperium (JS, on the client), and I have a situation where I need to register multiple ready handlers: one that marks the container I'm storing the data in as finished loading (so I can display it to the user), and another that checks if the currently active route matches one of the objects we just loaded, and re-runs the route handler if that is the case.
The thing is, using bucket.on(signal, handler) seems to overwrite the first applied handler with the second.
Only a single handler is supported at the moment. We'd welcome any feedback on this though.
I have an action/view that is going to be used for reporting purposes, no saving of data involved.
I want some form elements to be valid/invalid and if invalid, some errors to be shown.
What is the most CakePHPish way for showing errors in form fields that are not based on a model?
Even if you're not getting or saving your data from a database table, you still need a model for validation rules, that's the proper MVC way. You can set
var $useTable = false;
In your model if you're not planning on saving/reading anything. You can then use your model to just set your validation rules and messages.
You can validate your data without saving, within the controller like so:
$this->ModelName->validates()
For more information, please refer here: Validating Data from the Controller
So assume I have a class that has an init method that does something like... grabs some data off the net in xml format and parses it to initialize some of its properties. My concern is how should I handle the case where the network is down or the xml data my object receives is bad?
Normally in C i would use return values to indicate an error and what kind and then that would get propagated back till I could report it to the user. I don't really think that will work in this situation.
Use asynchronous network requests.
Create the UI and show it with
either dummy replacement for the
actual values (like pictures) or no
data (for example empty table).
Then create and send the request for
data and register handler that gets
called with data.
When you receive data your handler
gets called with them.
You parse the data and update the
UI. In case of data being invalid
you can now update UI to inform the
user.
You can use timeouts to cancel
requests in case of network problem
and functions not returning with
data within specific time.
There was an example in last year Stanford's CS193p class (iPhone programming but the same applies to desktop apps) with showing empty user interface and updating it when data coming back. You can probably find references to it on net or otherwise there'll be new example this year.
For network down you have a few options
Alert the user you cannot retrieve the needed data
Show stale (last loaded, maybe not stale?) data
For Bad Data:
Alert the user
Try again
Show old data
Try to fix the data (missing a closing tag? etc)
Show a subset of the data (maybe you can extract something that is usable?)
As far as error codes, you can do:
Return codes ie bad_data -1, no_network -2, etc.
You can throw exceptions, catch them and map them to user friendly display messages