NIHVIVO-646 Use autocomplete cache
This commit is contained in:
parent
dccd622b5b
commit
96d9b0508e
1 changed files with 100 additions and 90 deletions
|
@ -51,7 +51,7 @@ var addAuthorForm = {
|
|||
|
||||
this.initAutocomplete();
|
||||
|
||||
this.initAuthorReordering();
|
||||
this.initAuthorDD();
|
||||
|
||||
if (this.findValidationErrors()) {
|
||||
this.initFormAfterInvalidSubmission();
|
||||
|
@ -133,15 +133,20 @@ var addAuthorForm = {
|
|||
$('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() {
|
||||
$(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 ($('.authorship').length == 1) {
|
||||
addAuthorForm.disableAuthorDD();
|
||||
}
|
||||
// Reset the excluded uris in the autocomplete url so that the
|
||||
// author just removed is no longer excluded.
|
||||
$('#lastName').autocomplete('option', 'source', addAuthorForm.getAcUrl());
|
||||
//author just removed is no longer excluded.
|
||||
addAuthorForm.setAcUrl();
|
||||
});
|
||||
|
||||
// $(this).hide();
|
||||
|
@ -305,7 +310,7 @@ var addAuthorForm = {
|
|||
this.personUriField.val('');
|
||||
},
|
||||
|
||||
initAuthorReordering: function() {
|
||||
initAuthorDD: function() {
|
||||
|
||||
var authorshipList = $('#authorships'),
|
||||
authorships = authorshipList.children();
|
||||
|
@ -437,37 +442,39 @@ var addAuthorForm = {
|
|||
|
||||
initAutocomplete: function() {
|
||||
|
||||
//var cache = {};
|
||||
// RY change to this.cache = {}
|
||||
// then we'll have access to it when removing an author
|
||||
// Make cache a property of this so we can access it after removing
|
||||
// an author.
|
||||
this.acCache = {};
|
||||
this.baseAcUrl = $('.acUrl').attr('id');
|
||||
this.setAcUrl();
|
||||
|
||||
$('#lastName').autocomplete({
|
||||
minLength: 2,
|
||||
source: addAuthorForm.getAcUrl(),
|
||||
// RY For now, not using cache because there are complex interactions between filtering and caching.
|
||||
// We want to filter out existingAuthors from autocomplete results, and this seems easiest to do
|
||||
// server-side. But if an author gets removed, we need to put them back in the results. If results
|
||||
// are cached, the cache needs to be cleared on a remove.
|
||||
// source: function(request, response) {
|
||||
// if (request.term in cache) {
|
||||
// //console.log("found term in cache");
|
||||
// response(cache[request.term]);
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// $.ajax({
|
||||
// url: url,
|
||||
// dataType: 'json',
|
||||
// data: request,
|
||||
// complete: function(data) {
|
||||
// cache[request.term] = data;
|
||||
// console.log(data);
|
||||
// //console.log("not getting term from cache");
|
||||
// response(data);
|
||||
// }
|
||||
//
|
||||
// });
|
||||
// },
|
||||
source: function(request, response) {
|
||||
if (request.term in addAuthorForm.acCache) {
|
||||
console.log("found term in cache");
|
||||
response(addAuthorForm.acCache[request.term]);
|
||||
return;
|
||||
}
|
||||
console.log("not getting term from cache");
|
||||
|
||||
// If the url query params are too long, we could do a post
|
||||
// here instead of a get. Add the exclude uris to the data
|
||||
// rather than to the url.
|
||||
$.ajax({
|
||||
url: addAuthorForm.acUrl,
|
||||
dataType: 'json',
|
||||
data: request,
|
||||
complete: function(xhr) {
|
||||
// Not sure why, but we need an explicit json parse here. jQuery
|
||||
// should parse the response text and return an json object.
|
||||
var results = jQuery.parseJSON(xhr.responseText);
|
||||
addAuthorForm.acCache[request.term] = results;
|
||||
response(results);
|
||||
}
|
||||
|
||||
});
|
||||
},
|
||||
select: function(event, ui) {
|
||||
addAuthorForm.showSelectedAuthor(ui);
|
||||
}
|
||||
|
@ -475,15 +482,18 @@ var addAuthorForm = {
|
|||
|
||||
},
|
||||
|
||||
getAcUrl: function() {
|
||||
var url = $('.acUrl').attr('id'),
|
||||
setAcUrl: function() {
|
||||
var url = this.baseAcUrl,
|
||||
existingAuthors = $('#authorships .authorLink');
|
||||
|
||||
//console.log("in setAcUrl()");
|
||||
//console.log("number of existing authors: " + existingAuthors.length);
|
||||
|
||||
existingAuthors.each(function() {
|
||||
url += '&excludeUri=' + $(this).attr('id');
|
||||
});
|
||||
|
||||
return url;
|
||||
this.acUrl = url;
|
||||
},
|
||||
|
||||
prepareSubmit: function() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue