Improve accounts app using resources and events.

This commit is contained in:
Alexis Lahouze 2015-07-10 11:49:59 +02:00
parent bc933f77f9
commit 9d9d46fbee

View File

@ -14,29 +14,26 @@
You should have received a copy of the GNU Affero General Public License You should have received a copy of the GNU Affero General Public License
along with Accountant. If not, see <http://www.gnu.org/licenses/>. along with Accountant. If not, see <http://www.gnu.org/licenses/>.
*/ */
// Account object // vim: set tw=80 ts=2 sw=2 sts=2:
function account() {
this.id=ko.observable();
this.name=ko.observable();
this.authorized_overdraft=ko.observable();
this.future=ko.observable();
this.current=ko.observable();
this.pointed=ko.observable();
}
// The AccountController. // The AccountController.
accountantApp.controller( accountantApp
"AccountController", function($scope, $http, $rootScope, $window) {
// Account store and selection
$scope.account = null;
$scope.accounts = [];
$scope.savedAccount = null; .factory("Accounts", ["$resource", function($resource) {
$scope.editingAccount = null; return $resource(
$scope.removedAccount = null; "/api/accounts/:id", {
id: "@id"
}
);
}])
.controller(
"AccountController", [
"$scope", "$rootScope", "$window", "Accounts",
function($scope, $rootScope, $window, Accounts) {
$scope.accountClass = function(account) { $scope.accountClass = function(account) {
if(account == $scope.account) { if(account == $scope.selectedAccount) {
return "active"; return "active";
} }
}; };
@ -53,145 +50,165 @@ accountantApp.controller(
} }
}; };
$scope.addAccount = function() { /*
$scope.editingAccount = { * Create a new account and emit accountCreatedEvent.
id: null, */
name: null, $scope.createAccount = function(account) {
authorized_overdraft: null account.$save(function(account) {
// Reset new account.
$scope.resetAccount(account);
$scope.$emit("accountCreatedEvent", account);
});
}; };
angular.element("#edit-account").modal(); /*
}; * Notify on account created event.
*/
$rootScope.$on("accountCreatedEvent", function(e, account) {
new PNnotify({
type: "success",
title: "Save",
text: "Account #" + account.id + " created."
});
});
$scope.show = function() { /*
console.debug("show"); * Reload accounts on account created event.
}; */
$rootScope.$on("accountCreatedEvent", function(e, account) {
$scope.loadAccounts();
});
/*
* Start account edition.
*/
$scope.editAccount = function(account) { $scope.editAccount = function(account) {
if(account) { account.editing = true;
$scope.editingAccount = account;
$scope.savedAccount = angular.copy(account);
}
}; };
$scope.cancelEditAccount = function(account, modalScope) { /*
if($scope.savedAccount) { * Cancel account edition.
account.name = $scope.savedAccount.name; */
account.authorized_overdraft = $scope.savedAccount.authorized_overdraft; $scope.cancelEditAccount = function(account) {
}
$scope.editingAccount = null;
$scope.savedAccount = null;
if(modalScope && modalScope.dismiss) {
modalScope.dismiss();
}
};
$scope.saveAccount = function(account, modalScope) {
// Ajax call to save the entry.
var type;
var url = "/api/accounts";
if(account.id) { if(account.id) {
url += "/" + account.id; // Reload account if it is a saved one.
} account.$get();
$http.put(url, angular.toJson(account)).success(function(data) {
$.pnotify({
type: "success",
title: "Save",
text: data
});
$scope.editingAccount = null;
$scope.savedAccount = null;
if(modalScope && modalScope.dismiss) {
modalScope.dismiss();
}
// Reload accounts to update solds.
$scope.loadAccounts();
}).error(function(data) {
if(data.error_type == 'validation') {
angular.forEach(data.errors, function(errors, field) {
// $scope.form[field].$setValidity('server', false);
//$scope.errors[field] = errors.join(', ');
});
} else { } else {
$.pnotify({ // Reset the new account.
type: "error", $scope.newAccount = new Accounts();
title: "Save",
text: data.errors
});
} }
};
/*
* Save account and emit accountSavedEvent.
*/
$scope.saveAccount = function(account, modalScope) {
account.$save(function(data) {
$scope.$emit("accountSavedEvent", account);
}); });
}; };
// Function to remove of an entry. /*
$scope.accountRemove = function(account, modalScope) { * Notify on account saved event.
$http.delete("/api/accounts/" + account.id).success(function (data) { */
$.pnotify({ $rootScope.$on("accountSavedEvent", function(e, account) {
new PNnotify({
type: "success", type: "success",
title: "Save", title: "Save",
text: data text: "Account #" + account.id + " saved."
});
}); });
// Reload accounts to update solds. /*
$scope.loadAccounts(); * Delete an account and emut accountDeletedEvent.
*/
if(modalScope && modalScope.dismiss) { $scope.deleteAccount = function(account, modalScope) {
modalScope.dismiss(); account.$delete(function(data) {
} $scope.$emit("accountDeletedEvent", account);
}); });
}; };
// Function to load accounts /*
$scope.loadAccounts = function() { * Notify on account deleted event.
$http.get("/api/accounts").success(function (data) { */
// Update accounts $rootScope.$on("accountDeletedEvent", function(e, account) {
$scope.accounts = angular.fromJson(data); new PNotify({
type: "success",
title: "Save",
text: "Account #" + account.id + " deleted."
});
});
/*
* Reload acounts when an account is deleted.
*/
$rootScope.$on("accountDeletedEvent", function(e, account) {
// Reset the selected account if this is the one whe have deleted.
if($scope.selectedAccount.id == account.id) {
$scope.selectedAccount = null;
}
$scope.loadAccounts();
});
/*
* Reload accounts and emit accountsLoadedEvent.
*/
$scope.loadAccounts = function() {
// Reset the new account.
$scope.newAccount = new Accounts();
// Load accounts using the resource.
$scope.accounts = Accounts.query(function (data) {
$scope.$emit("accountsLoadedEvent", $scope.accounts); $scope.$emit("accountsLoadedEvent", $scope.accounts);
}); });
}; };
// Callback function to select a new account. /*
$scope.selectAccount = function(account) { * Update the selected account with loaded one or select the first of the
if(account) { * list.
$scope.account = account; */
$scope.$emit("accountSelectedEvent", {account: $scope.account});
}
};
$rootScope.$on("accountsLoadedEvent", function(e, accounts) { $rootScope.$on("accountsLoadedEvent", function(e, accounts) {
var accountToSelect = null; var accountToSelect = null;
// Reset selected account to the new instance corresponding to the old one. // Reset selected account to the new instance corresponding to the old
if($scope.account) { // one.
if($scope.selectedAccount) {
// Find the new instance of the previously selected account. // Find the new instance of the previously selected account.
angular.forEach($scope.accounts, function(account) { angular.forEach($scope.accounts, function(account) {
if(account.id == $scope.account.id) { if(account.id == $scope.selectedAccount.id) {
accountToSelect = account; // No need to emit accountSelectedEvent.
$scope.selectedAccount = account;
} }
}); });
} } else {
// Set selected account to first one if not yet selected // Set selected account to first one if not yet selected
if(!accountToSelect && $scope.accounts.length > 0){ $scope.selectAccount($scope.accounts[0]);
accountToSelect = $scope.accounts[0];
} }
$scope.selectAccount(accounts[0]);
}); });
// TODO Alexis Lahouze 2015-06-16 Replace by refresh account. /*
$rootScope.$on("entrySavedEvent", $scope.loadAccounts); * Select the account to display and emit accountSelectedEvent.
$rootScope.$on("entryRemovedEvent", $scope.loadAccounts); */
$rootScope.$on("operationSavedEvent", $scope.loadAccounts); $scope.selectAccount = function(account) {
$rootScope.$on("operationRemovedEvent", $scope.loadAccounts); $scope.selectedAccount = account;
$scope.$emit("accountSelectedEvent", {account: account});
};
/*
* Refresh account information.
*/
$scope.refreshAccount = function() {
$scope.selectedAccount.$get();
};
$rootScope.$on("entrySavedEvent", $scope.refreshAccount);
$rootScope.$on("entryCreatedEvent", $scope.refreshAccount);
$rootScope.$on("entryRemovedEvent", $scope.refreshAccount);
//$rootScope.$on("operationSavedEvent", $scope.refreshAccount);
//$rootScope.$on("operationRemovedEvent", $scope.refreshAccount);
$scope.loadAccounts(); $scope.loadAccounts();
}); }]);