Skip to content

Instantly share code, notes, and snippets.

@alextoul
Created November 11, 2011 11:01
Show Gist options
  • Save alextoul/1357745 to your computer and use it in GitHub Desktop.
Save alextoul/1357745 to your computer and use it in GitHub Desktop.
jQuery JSON call best practice
$("#show_teams").live("pagebeforeshow",
function(event,ui)
{
$.ajax({"url":'http://hockey-community.com/api/users/<%= @current_user.username %>/teams',"dataType":"json","async": false,"context":this,
"error":onError(), "success": function(teams)
{
console.log("calling API for teams");
if (teams.length > 0){
$.each(teams, function() {
console.log('loading team'+this.team.name);
$("#teams_listing").append('<div><a href="'+this.team.link+'" class="ui-link"><img alt="'+this.team.name+'" height="65" src="'+this.team.profile_picture+'" width="65"></a><span>'+this.team.name+'</span></div>');
});
}
else {
$('#teams_listing').html("<p>You're not in a team for now but you can join or create one on our website.</p><br>Please visit http://hockey-community.com from your computer.");
}
$('#show_teams').page('refresh');
}
});
});
@searchtool
Copy link

Why do you say "async": false ?
I use the default value (true)
read Jquery doc :
"By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment