NIHVIVO-646 Use autocomplete cache

This commit is contained in:
rjy7 2010-07-04 13:58:27 +00:00
parent dccd622b5b
commit 96d9b0508e

View file

@ -51,7 +51,7 @@ var addAuthorForm = {
this.initAutocomplete(); this.initAutocomplete();
this.initAuthorReordering(); this.initAuthorDD();
if (this.findValidationErrors()) { if (this.findValidationErrors()) {
this.initFormAfterInvalidSubmission(); this.initFormAfterInvalidSubmission();
@ -133,15 +133,20 @@ var addAuthorForm = {
$('input#rank').val(rank); $('input#rank').val(rank);
} }
// In future, do this selectively by only clearing terms that match the
// deleted author's name
addAuthorForm.acCache = {};
authorship.fadeOut(400, function() { authorship.fadeOut(400, function() {
$(this).remove(); $(this).remove();
// Actions that depend on the author having been removed from the DOM:
// If there's just one author remaining, disable drag-drop // If there's just one author remaining, disable drag-drop
if ($('.authorship').length == 1) { if ($('.authorship').length == 1) {
addAuthorForm.disableAuthorDD(); addAuthorForm.disableAuthorDD();
} }
// Reset the excluded uris in the autocomplete url so that the // Reset the excluded uris in the autocomplete url so that the
//author just removed is no longer excluded. //author just removed is no longer excluded.
$('#lastName').autocomplete('option', 'source', addAuthorForm.getAcUrl()); addAuthorForm.setAcUrl();
}); });
// $(this).hide(); // $(this).hide();
@ -305,7 +310,7 @@ var addAuthorForm = {
this.personUriField.val(''); this.personUriField.val('');
}, },
initAuthorReordering: function() { initAuthorDD: function() {
var authorshipList = $('#authorships'), var authorshipList = $('#authorships'),
authorships = authorshipList.children(); authorships = authorshipList.children();
@ -437,37 +442,39 @@ var addAuthorForm = {
initAutocomplete: function() { initAutocomplete: function() {
//var cache = {}; // Make cache a property of this so we can access it after removing
// RY change to this.cache = {} // an author.
// then we'll have access to it when removing an author this.acCache = {};
this.baseAcUrl = $('.acUrl').attr('id');
this.setAcUrl();
$('#lastName').autocomplete({ $('#lastName').autocomplete({
minLength: 2, minLength: 2,
source: addAuthorForm.getAcUrl(), source: function(request, response) {
// RY For now, not using cache because there are complex interactions between filtering and caching. if (request.term in addAuthorForm.acCache) {
// We want to filter out existingAuthors from autocomplete results, and this seems easiest to do console.log("found term in cache");
// server-side. But if an author gets removed, we need to put them back in the results. If results response(addAuthorForm.acCache[request.term]);
// are cached, the cache needs to be cleared on a remove. return;
// source: function(request, response) { }
// if (request.term in cache) { console.log("not getting term from cache");
// //console.log("found term in cache");
// response(cache[request.term]); // If the url query params are too long, we could do a post
// return; // here instead of a get. Add the exclude uris to the data
// } // rather than to the url.
// $.ajax({
// $.ajax({ url: addAuthorForm.acUrl,
// url: url, dataType: 'json',
// dataType: 'json', data: request,
// data: request, complete: function(xhr) {
// complete: function(data) { // Not sure why, but we need an explicit json parse here. jQuery
// cache[request.term] = data; // should parse the response text and return an json object.
// console.log(data); var results = jQuery.parseJSON(xhr.responseText);
// //console.log("not getting term from cache"); addAuthorForm.acCache[request.term] = results;
// response(data); response(results);
// } }
//
// }); });
// }, },
select: function(event, ui) { select: function(event, ui) {
addAuthorForm.showSelectedAuthor(ui); addAuthorForm.showSelectedAuthor(ui);
} }
@ -475,15 +482,18 @@ var addAuthorForm = {
}, },
getAcUrl: function() { setAcUrl: function() {
var url = $('.acUrl').attr('id'), var url = this.baseAcUrl,
existingAuthors = $('#authorships .authorLink'); existingAuthors = $('#authorships .authorLink');
//console.log("in setAcUrl()");
//console.log("number of existing authors: " + existingAuthors.length);
existingAuthors.each(function() { existingAuthors.each(function() {
url += '&excludeUri=' + $(this).attr('id'); url += '&excludeUri=' + $(this).attr('id');
}); });
return url; this.acUrl = url;
}, },
prepareSubmit: function() { prepareSubmit: function() {