Add select2 cache for complete group list
To avoid making a server request every time the dropdown opens, the whole list of groups are cached (from the last request): Whenever the user types in a search term it will still send server requests.
This commit is contained in:
parent
8d0d336e03
commit
71776e307f
|
@ -5,6 +5,9 @@
|
|||
*/
|
||||
OC.Settings = OC.Settings || {};
|
||||
OC.Settings = _.extend(OC.Settings, {
|
||||
|
||||
_cachedGroups: null,
|
||||
|
||||
/**
|
||||
* Setup selection box for group selection.
|
||||
*
|
||||
|
@ -16,6 +19,7 @@ OC.Settings = _.extend(OC.Settings, {
|
|||
* @param [extraOptions] extra options hash to pass to select2
|
||||
*/
|
||||
setupGroupsSelect: function($elements, extraOptions) {
|
||||
var self = this;
|
||||
if ($elements.length > 0) {
|
||||
// note: settings are saved through a "change" event registered
|
||||
// on all input fields
|
||||
|
@ -24,16 +28,23 @@ OC.Settings = _.extend(OC.Settings, {
|
|||
allowClear: true,
|
||||
multiple: true,
|
||||
separator: '|',
|
||||
ajax: {
|
||||
url: OC.generateUrl('/settings/ajax/grouplist'),
|
||||
dataType: 'json',
|
||||
quietMillis: 100,
|
||||
data: function (term) {
|
||||
return {
|
||||
pattern: term, //search term
|
||||
query: _.debounce(function(query) {
|
||||
var queryData = {};
|
||||
if (self._cachedGroups && query.term === '') {
|
||||
query.callback({results: self._cachedGroups});
|
||||
return;
|
||||
}
|
||||
if (query.term !== '') {
|
||||
queryData = {
|
||||
pattern: query.term,
|
||||
filterGroups: 1
|
||||
};
|
||||
},
|
||||
results: function (data) {
|
||||
}
|
||||
$.ajax({
|
||||
url: OC.generateUrl('/settings/ajax/grouplist'),
|
||||
data: queryData,
|
||||
dataType: 'json',
|
||||
success: function(data) {
|
||||
if (data.status === "success") {
|
||||
var results = [];
|
||||
|
||||
|
@ -45,12 +56,17 @@ OC.Settings = _.extend(OC.Settings, {
|
|||
results.push({id:group.id, displayname:group.name});
|
||||
});
|
||||
|
||||
return {results: results};
|
||||
if (query.term === '') {
|
||||
// cache full list
|
||||
self._cachedGroups = results;
|
||||
}
|
||||
query.callback({results: results});
|
||||
} else {
|
||||
//FIXME add error handling
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
}, 100, true),
|
||||
id: function(element) {
|
||||
return element.id;
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue