Cache the results of the AJAX calls, for faster response.

This commit is contained in:
j2blake 2011-07-14 19:15:31 +00:00
parent ac163cb8dd
commit 63c4d2bbc8

View file

@ -54,17 +54,16 @@ var associateProfileFields = {
}, },
bindEventListeners: function() { bindEventListeners: function() {
this.idCache = {};
this.externalAuthIdField.change(function() { this.externalAuthIdField.change(function() {
associateProfileFields.externalAuthIdFieldHasChanged(); associateProfileFields.externalAuthIdFieldHasChanged();
}); });
this.externalAuthIdField.keyup(function() { this.externalAuthIdField.keyup(function() {
associateProfileFields.externalAuthIdFieldHasChanged(); associateProfileFields.externalAuthIdFieldHasChanged();
}); });
this.externalAuthIdField.bind("propertychange", function() { this.externalAuthIdField.bind("propertychange", function() {
associateProfileFields.externalAuthIdFieldHasChanged(); associateProfileFields.externalAuthIdFieldHasChanged();
}); });
this.externalAuthIdField.bind("input", function() { this.externalAuthIdField.bind("input", function() {
associateProfileFields.externalAuthIdFieldHasChanged(); associateProfileFields.externalAuthIdFieldHasChanged();
}); });
@ -84,9 +83,14 @@ var associateProfileFields = {
associateProfileFields.newProfileClassHasChanged(); associateProfileFields.newProfileClassHasChanged();
}); });
this.acCache = {};
this.associateProfileNameField.autocomplete({ this.associateProfileNameField.autocomplete({
minLength: 3, minLength: 3,
source: function(request, response) { source: function(request, response) {
if (request.term in associateProfileFields.acCache) {
response(associateProfileFields.acCache[request.term]);
return;
}
$.ajax({ $.ajax({
url: associateProfileFields.ajaxUrl, url: associateProfileFields.ajaxUrl,
dataType: 'json', dataType: 'json',
@ -97,6 +101,7 @@ var associateProfileFields = {
}, },
complete: function(xhr, status) { complete: function(xhr, status) {
var results = jQuery.parseJSON(xhr.responseText); var results = jQuery.parseJSON(xhr.responseText);
associateProfileFields.acCache[request.term] = results;
response(results); response(results);
} }
}); });
@ -120,32 +125,45 @@ var associateProfileFields = {
}, },
externalAuthIdFieldHasChanged: function() { externalAuthIdFieldHasChanged: function() {
if (this.externalAuthIdField.val().length == 0) { var externalAuthId = this.externalAuthIdField.val();
if (externalAuthId.length == 0) {
this.hideAllOptionals(); this.hideAllOptionals();
return; return;
} }
if (externalAuthId in this.idCache) {
var results = this.idCache[externalAuthId];
this.applyAjaxResultsForExternalAuthIdField(results)
return;
}
$.ajax({ $.ajax({
url: associateProfileFields.ajaxUrl, url: associateProfileFields.ajaxUrl,
dataType: "json", dataType: "json",
data: { data: {
action: "checkExternalAuth", action: "checkExternalAuth",
userAccountUri: associateProfileFields.userUri, userAccountUri: associateProfileFields.userUri,
externalAuthId: associateProfileFields.externalAuthIdField.val() externalAuthId: externalAuthId
}, },
complete: function(xhr, status) { complete: function(xhr, status) {
var results = $.parseJSON(xhr.responseText); var results = $.parseJSON(xhr.responseText);
if (results.idInUse) { associateProfileFields.idCache[externalAuthId] = results;
associateProfileFields.showExternalAuthInUseMessage() associateProfileFields.applyAjaxResultsForExternalAuthIdField(results);
} else if (results.matchesProfile) {
associateProfileFields.showAssociatedProfileArea(results.profileLabel, results.profileUri, results.profileUrl)
} else {
associateProfileFields.showAssociatingOptionsArea();
}
} }
}); });
}, },
applyAjaxResultsForExternalAuthIdField: function(results) {
if (results.idInUse) {
this.showExternalAuthInUseMessage()
} else if (results.matchesProfile) {
this.showAssociatedProfileArea(results.profileLabel, results.profileUri, results.profileUrl)
} else {
this.showAssociatingOptionsArea();
}
},
openVerifyWindow: function() { openVerifyWindow: function() {
window.open(this.verifyUrl, 'verifyMatchWindow', 'width=640,height=640,scrollbars=yes,resizable=yes,status=yes,toolbar=no,menubar=no,location=no'); window.open(this.verifyUrl, 'verifyMatchWindow', 'width=640,height=640,scrollbars=yes,resizable=yes,status=yes,toolbar=no,menubar=no,location=no');
}, },