Skip to content

Instantly share code, notes, and snippets.

@tischler
Created May 2, 2012 20:51
Show Gist options
  • Save tischler/2580395 to your computer and use it in GitHub Desktop.
Save tischler/2580395 to your computer and use it in GitHub Desktop.
Tim's Code read
function render_servers(environment_name) {
// ...
jQuery.getJSON(url, function(data) {
// you are using val as your incoming variable here, and also below
// $.each(data.servers, function(key, val) {
$.each(data.servers, function(server_name, server_data) {
// why are you using var $var_name here? $ is either for mechanically generated code/vars or
// for jquery selectors: var $my_buttons = $(".buttons")
// no big deal, but javascripters would be all wtf...
var $server_id = server_data.id;
// you are using this as a true/false - just make it a boolean
var checked=false; // var checked = '';
searching_for = $("#application_name").val().replace(/[_]/g, "")) // this is expensive(ish)
jQuery.getJSON(server_instances_url, function(data) {
// using val here.. i also vote for being more explicit and avoid scoping errors or questions
$.each(data.instances, function(instance_name, instance_data) {
if(instance_data.parent_application_name === searching_for) {
checked = true; // we found it!!!
}
});
});
alert(checked);
// BUG!!! BUG!!!!!
// you are declaring input inside the block, which means this value WILL NOT get outside of the block!!!!
var my_cool_input = "";
if(checked) { // } else if(checked.match("checked")) {
var my_cool_input = "i was checked!!";
var $input = "<input " + checked + " class='server_checkbox' id=" + val.id + " name=servers[" + environment_name + "][] type='checkbox' value=" + val.id + "><label for=" + val.id + ">" + val.hostname + "</label>";
// otherwise just make a standard input
} else {
my_cool_input = "i was not checked... :(";
var $input = "<input class='server_checkbox' id=" + val.id + " name=servers[" + environment_name +"][] type='checkbox' value=" + val.id + "><label for=" + val.id + ">" + val.hostname + "</label>";
}
alert(my_cool_input); // will print something guaranteed
alert(input); // will be undefined, because there is no input within scope
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment