Added used objects. Commented. Refactored account and month using ko.mapping.
This commit is contained in:
parent
f19b6488b0
commit
1a6fa5de0d
@ -11,12 +11,28 @@ function entry(){
|
||||
this.category=ko.observable();
|
||||
}
|
||||
|
||||
// Account object
|
||||
function account() {
|
||||
this.id=ko.observable();
|
||||
this.name=ko.observable();
|
||||
this.future=ko.observable();
|
||||
this.current=ko.observable();
|
||||
this.pointed=ko.observable();
|
||||
}
|
||||
|
||||
// Month object
|
||||
function month() {
|
||||
this.year=ko.observable();
|
||||
this.month=ko.observable();
|
||||
}
|
||||
|
||||
// Util function to show a message in message placeholder.
|
||||
function message(alertType, title, message) {
|
||||
$(".alert").alert('close');
|
||||
$("#message-placeholder").append('<div class="alert alert-' + alertType + '"><button type="button" class="close" data-dismiss="alert">×</button><h4>' + title + '</h4><strong>' + message + '</strong></div>');
|
||||
}
|
||||
|
||||
// The ListViewModel used to instanciate viewmodel.
|
||||
var ListViewModel = function() {
|
||||
var self = this;
|
||||
|
||||
@ -120,53 +136,74 @@ var ListViewModel = function() {
|
||||
|
||||
// Function to load entries from server for a specific account and month.
|
||||
self.loadEntries = function(account, month) {
|
||||
$.post("api/entry.php", {action: "get_entries", account: account.id, year: month.year, month: month.month}, function(data) {
|
||||
// Clean up current entries.
|
||||
self.entries.removeAll();
|
||||
|
||||
$.post("api/entry.php", {action: "get_entries", account: account.id(), year: month.year(), month: month.month()}, function(data) {
|
||||
// Clean up selected entry.
|
||||
self.selectedItem(null);
|
||||
|
||||
// Update entries
|
||||
self.entries(ko.utils.arrayMap(data, ko.mapping.fromJS));
|
||||
});
|
||||
};
|
||||
|
||||
// Function to load accounts
|
||||
self.loadAccounts = function() {
|
||||
$.post("api/entry.php", {action: "get_accounts"}).success(function (result) {
|
||||
self.accounts(result);
|
||||
$.post("api/entry.php", {action: "get_accounts"}).success(function (data) {
|
||||
// Update accounts
|
||||
self.accounts(ko.utils.arrayMap(data, ko.mapping.fromJS));
|
||||
|
||||
// Reset selected account to the new instance corresponding to the old one.
|
||||
if(self.account()) {
|
||||
var oldId = self.account().id();
|
||||
|
||||
// Reset to null
|
||||
self.account(null);
|
||||
|
||||
// Find the new instance of the previously selected account.
|
||||
$.each(self.accounts(), function(index, account) {
|
||||
if(self.account().id == account.id) {
|
||||
if(account.id() == oldId) {
|
||||
self.account(account);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Set selected account to first one if not yet selected
|
||||
if(!self.account()){
|
||||
self.account(result[0]);
|
||||
self.account(self.accounts()[0]);
|
||||
}
|
||||
|
||||
// Load months
|
||||
self.loadMonths(self.account());
|
||||
});
|
||||
};
|
||||
|
||||
// Function to load months
|
||||
self.loadMonths = function(account){
|
||||
$.post("api/entry.php", {action: "get_months", account: account.id}).success(function (result) {
|
||||
self.months(result);
|
||||
$.post("api/entry.php", {action: "get_months", account: account.id()}).success(function (data) {
|
||||
// Update months
|
||||
self.months(ko.utils.arrayMap(data, ko.mapping.fromJS));
|
||||
|
||||
// Reset selected month to the new instance corresponding to the old one
|
||||
if(self.month()) {
|
||||
var oldYear = self.month().year();
|
||||
var oldMonth = self.month().month();
|
||||
|
||||
// Reset to null
|
||||
self.month(null);
|
||||
|
||||
// Find the new instance of the previously selected month.
|
||||
$.each(self.months(), function(index, month) {
|
||||
if(self.month().year == month.year && self.month().month == month.month) {
|
||||
if(month.year() == oldYear && month.month() == oldMonth) {
|
||||
self.month(month);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Set selected month to the last one if not yet selected.
|
||||
if(!self.month()) {
|
||||
self.month(result[result.length - 1]);
|
||||
self.month(self.months()[self.months().length - 1]);
|
||||
}
|
||||
|
||||
// Load entries
|
||||
self.loadEntries(self.account(), self.month());
|
||||
});
|
||||
};
|
||||
@ -213,7 +250,7 @@ var ListViewModel = function() {
|
||||
|
||||
self.add = function() {
|
||||
var newEntry = new entry();
|
||||
newEntry.account_id(self.account().id);
|
||||
newEntry.account_id(self.account().id());
|
||||
|
||||
self.entries.unshift(newEntry);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user