Changed reload behaviour after save or delete.

This commit is contained in:
Alexis Lahouze 2013-01-10 13:10:41 +01:00
parent a41cf30723
commit 41618f3096

View File

@ -42,8 +42,8 @@ var ListViewModel = function() {
self.selectedItem = ko.observable(); self.selectedItem = ko.observable();
self.savedItem = ko.observable(); self.savedItem = ko.observable();
self.loadEntries = function() { self.loadEntries = function(account, month) {
$.post("api/entry.php", {action: "get_entries", account: self.account().id, year: self.month().year, month:self.month().month}, function(data) { $.post("api/entry.php", {action: "get_entries", account: account.id, year: month.year, month: month.month}, function(data) {
self.entries.removeAll(); self.entries.removeAll();
self.selectedItem(null); self.selectedItem(null);
@ -69,6 +69,8 @@ var ListViewModel = function() {
if(!self.account()){ if(!self.account()){
self.account(result[0]); self.account(result[0]);
} }
self.loadMonths(self.account());
}); });
}; };
@ -76,7 +78,11 @@ var ListViewModel = function() {
$.post("api/entry.php", {action: "get_months", account: account.id}).success(function (result) { $.post("api/entry.php", {action: "get_months", account: account.id}).success(function (result) {
self.months(result); self.months(result);
self.month(result[result.length - 1]); if(!self.month()) {
self.month(result[result.length - 1]);
}
self.loadEntries(self.account(), self.month());
}); });
}; };
@ -158,7 +164,7 @@ var ListViewModel = function() {
message("success", "Save", data.message); message("success", "Save", data.message);
self.selectedItem(null); self.selectedItem(null);
self.loadEntries(); self.loadAccounts();
}).error(function() { }).error(function() {
message("error", "Error.", "Unexpected error."); message("error", "Error.", "Unexpected error.");
}); });
@ -178,17 +184,19 @@ var ListViewModel = function() {
}; };
self.selectMonth = function(month) { self.selectMonth = function(month) {
self.month(month); if(month) {
self.month(month);
self.loadEntries(self.account(), month);
}
}; };
self.selectAccount = function(account) { self.selectAccount = function(account) {
self.account(account); if(account) {
self.account(account);
self.loadMonths(account);
}
}; };
self.account.subscribe(self.loadMonths);
self.month.subscribe(self.loadEntries);
$(document).ready(self.loadAccounts); $(document).ready(self.loadAccounts);
}; };