I have an object constructor:
function Context(tid) {
this.tid = tid;
var self = this;
//We get all the data we need from the server to use in this Context.
$.post(
'/review/context/get_context',
{tid: tid},
function(data) {
results = $.parseJSON(data);
if(results.status == "SUCCESS") {
self.obj = results.object;
self.currenttodo = results.tid
}
}
);
}
I want to initiate that on a document load, and use that object, until and event triggers the object to be changed, at which point I want to wipe that object and re-initiate it with info from a different ajax call. It's desirable to use an object here because there is a boatload of functions that apply to all Contexts.
$(document).ready(function() {
cc = new Context($('#context-tabs').attr('name'));
console.log(cc);
console.log(cc.currenttodo);
}
generates the following console output:
Context
currenttodo: 14
obj: Object
tid: "1"
__proto__: Context
undefined
I think this is a standard case of this being an asynchonous function not being complete before execution of subsequent function, but I have a few questions:
1) Why can I see the value of cc.currenttodo in the console.log directly before the console.log where I can't access it? 2) Why is the Chrome console.log output different for two integers. The 14 is not in quotes and blue, and the tid: "1" is red and in quotes. I presume that indicates that tid is a string, but I want to confirm that. 3) How, if I can't use asynchronous calls in this manner would I instantiate a unique object for use in the calling $document.ready() function populated from a server call? I would think I would need to do it through the success: function, but can't seem to get anything except self.variables out of there?
Thanks in advance.
UPDATE: SO won't let me answer my own questions yet, so here is the working code:
function Context(tid) {
this.tid = tid;
var self = this;
//We get all the data we need from the server to use in this Context.
$.ajax({
url: "/review/context/get_context",
type: "POST",
data: "tid="+self.tid,
async: false,
dataType: "json",
success: function(data) {
if(data.status == "SUCCESS") {
self.obj = data.object;
self.currenttodo = data.tid;
}
}
});
}