I'm trying to get a handle on javascript and prototype (not the library) to create a reusable script that will ultimately output a nice, formatted table of data from a database. I'm able to use jQuery AJAX to get the data in JSON format, and generate the table without issue. Ideally, it'll be sortable and paged, and that's where the trouble starts.
I've tried a few methods of construction, but it seems that using javascript's prototype is what I want, if I'm understanding it correctly. What I seem to be misunderstanding (at the very least) is data persistence when the user takes action by clicking a link or a button.
Here's a short for which I've created to illustrate the issue:
http://jsfiddle.net/Scopique/yrt3krn9/
//Uses jQuery 1.11.0 because IE
var Outside = function () {
this.MyVariable = 'Hi';
this.PersistVariables = 'Oops';
}
Outside.prototype.GenerateHTML = function () {
var that = this;
//Value pops as expected
alert(that.PersistVariables);
var localHTML = "<a href='#' id='link' onclick='Outside.prototype.Respond(\"" + that.MyVariable + "\");'>Click Me</a>";
$("#output").html(localHTML);
}
Outside.prototype.Respond = function (whatVariable) {
var that = this;
alert(whatVariable);
//Value does not pop...
alert(that.PersistVariables);
}
$(document).ready(function () {
var out = new Outside();
out.GenerateHTML();
});
And beyond this there's just:
<div id="output"></div>
In this example, the value of MyVariable is visible when Outside.prototype.Respond is trigged through clicking the link that's added to the DIV at run time as it's being explicitly handed down through the process of definition, building the HTML, and passed as an argument to the method.
However, PersistVariables always comes back as undefined when Respond is called. It's assigned a value in the constructor, and it displays the proper value when GenerateHTML is called -- a natural execution flow, I'm supposing -- but the value isn't understood in Respond after the link has been clicked.
I could ask "what am I missing" but I suspect that it's not that simple :D Are the variables that I want to access scoped correctly? If so, what is it about the link click that causes them to lose their minds post-link click.
Thanks!