I'm kinda struggling with Javascript variable scopes in combination with a jQuery JSON call. Unforutnatly, I can't post this script on jsFiddle, because it needs some data.
I'm trying to dynamicly load data and output it on the user's screen. I've now only created the method that loads the data for the first time. Later on I'd like to make an update method which updates the data in the table rows.
But now I'm having a problem with the variable scope. When $.pcw.outstandingInvoices.init() has been called, I get the following error:
ypeError: can't convert undefined to object
this.outstandingInvoicesArray[key] = entry;
My code:
-- Post is getting to long, so removed the first code i've used. --
Could anyone tell me what I'm doing wrong so I can fix it?
Thanks in advance!
--- UPDATE --- I've edited the things you guys told me, but still I'm getting errors... Could anyone tell me what I'm doing wrong?
My new code and errors:
-- Post is getting to long, so removed the first update of the code. --
Errors:
Initializing outstandingInvoices class.
Loading outstanding invoices from server.
TypeError: context.outstandingInvoicesObject is undefined
if (context.outstandingInvoicesObject.length == 0) {
TypeError: can't convert undefined to object
self.outstandingInvoicesObject[key] = entry;
-- UPDATE 2 -- Just edited my code and now I'm not gettting errors, but the data in outstandingInvoicesObject is not properly saved, so the method addOutstandingInvoicesTable can't find any invoices. I've been analyzing the console and it seems the there is somethin wrong with order of execution of the methods...
The code:
$.pcw.outstandingInvoices = function () {
var context = this;
context.outstandingInvoicesObject = [];
context.init = function ()
{
console.log('Initializing outstandingInvoices class.');
context.loadData();
context.removeLoader();
context.addOutstandingInvoicesToTable();
};
context.loadData = function ()
{
console.log('Loading outstanding invoices from server.');
jQuery.getJSON('/ajax/outgoing-invoices/find-outstanding.json', function (data)
{
var i = 0;
jQuery.each(data, function (key, entry)
{
context.outstandingInvoicesObject[key] = entry;
++i;
});
console.log('Loaded ' + i + ' invoices');
}).error(function () {
console.error('Error while loading outstanding invoices.');
});
};
context.removeLoader = function ()
{
console.log('Removing loader');
jQuery('table#invoices-outstanding tr.ajax-loader').fadeOut();
};
context.addOutstandingInvoicesToTable = function()
{
console.log('Outputing invoices to table');
if (context.outstandingInvoicesObject.length == 0) {
console.log('No outstanding invoices found');
}
jQuery.each(context.outstandingInvoicesObject, function (key, entry)
{
// This is a new outstanding invoice
var rowClass = 'info';
switch(entry.status)
{
case 'Created':
case 'Sent':
case 'Paid': // The invoices with Paid statuses aren't show here, because we only show the oustanding invoices on this page.
rowClass = 'success';
break;
case 'First reminder':
case 'Second reminder':
case 'Partially paid':
rowClass = 'warning';
break;
case 'Third reminder':
case 'Collection agency':
case 'Judicial proceedings':
rowClass = 'error';
break;
}
jQuery('table#invoices-outstanding tbody').append(
outstandingInvoice = jQuery('<tr/>', {
id: 'outgoing-invoice-' + key,
class: rowClass
}).append(
jQuery('<td/>', {
class: 'id',
text: key
})
).append(
jQuery('<td/>', {
class: 'debtor-name',
text: entry.debtor_name
})
).append(
jQuery('<td/>', {
class: 'amount',
text: entry.amount
})
).append(
jQuery('<td/>', {
class: 'date',
text: entry.created_timestamp
})
).append(
jQuery('<td/>', {
class: 'status',
text: entry.status
})
).append(
jQuery('<td/>', {
class: 'creator',
text: entry.creator
})
)
);
});
}
}
// When document is ready
(function()
{
var invoices = new $.pcw.outstandingInvoices();
invoices.init();
})();
The console output:
Initializing outstandingInvoices class.
Loading outstanding invoices from server.
GET /ajax/outgoing-invoices/find-outstanding.json
200 OK
509ms
Removing loader
Outputing invoices to table
No outstanding invoices found
Loaded 29 invoices
Thanks