Improve account selection.

This commit is contained in:
Alexis Lahouze 2015-06-17 00:28:41 +02:00
parent 67db964546
commit eea61a3a9b

View File

@ -149,13 +149,24 @@ accountantApp.controller(
// Function to load accounts
$scope.loadAccounts = function() {
$http.get("/api/accounts").success($scope.loadAccounts_success);
};
$scope.loadAccounts_success = function (data) {
$http.get("/api/accounts").success(function (data) {
// Update accounts
$scope.accounts = angular.fromJson(data);
$scope.$emit("accountsLoadedEvent", $scope.accounts);
});
};
// Callback function to select a new account.
$scope.selectAccount = function(account) {
if(account) {
$scope.account = account;
$scope.$emit("accountSelectedEvent", {account: $scope.account});
}
};
$rootScope.$on("accountsLoadedEvent", function(e, accounts) {
var accountToSelect = null;
// Reset selected account to the new instance corresponding to the old one.
@ -173,21 +184,10 @@ accountantApp.controller(
accountToSelect = $scope.accounts[0];
}
// Reset to account to select
$scope.account = accountToSelect;
$scope.$emit("accountsLoadedEvent", {account: $scope.account});
};
// Callback function to select a new account.
$scope.selectAccount = function(account) {
if(account) {
$scope.account = account;
$scope.$emit("accountsLoadedEvent", {account: $scope.account});
}
};
$scope.selectAccount(accounts[0]);
});
// TODO Alexis Lahouze 2015-06-16 Replace by refresh account.
$rootScope.$on("entrySavedEvent", $scope.loadAccounts);
$rootScope.$on("entryRemovedEvent", $scope.loadAccounts);
$rootScope.$on("operationSavedEvent", $scope.loadAccounts);