Handling when stocks are updated
Earlier, in the online quotes service, we fired a global event every time we retrieved the latest price of a stock. We must now implement some code to be executed when such an event is fired. Inside the ApplicationWindow.js
file, right at the end of the ApplicationWindow
function, we will add a new event listener for the oqs:stockUpdated
global event:
Ti.App.addEventListener('oqs:stockUpdated', function(stock) {
The first thing we will do is update the stock using the preference service:
ps.updateStock(stock);
We will then update the progress bar's attributes using the newly calculated values:
progress.value = ps.getPortfolioValue(); progress.max = ps.getObjective(); progress.show();
Finally, we will determine if our user has reached his/her objective. If that is the case, we change the big label's value to SELL
:
if (progress.value < progress.max) { lblWhatToDo.text = 'HOLD'; } else { lblWhatToDo.text = '! SELL !'; } });