Browse Source

Add parse response method, add additional debugging, dont instantiate API on each query #1634

pull/1698/merge
jlukic 9 years ago
parent
commit
5f18f2c1c3
1 changed files with 68 additions and 33 deletions
  1. 101
      src/definitions/modules/search.js

101
src/definitions/modules/search.js

@ -256,6 +256,21 @@ $.fn.search = function(parameters) {
} }
}, },
setup: {
api: function() {
var
apiSettings = {
on : false,
action : 'search',
debug : settings.debug,
onFailure : module.error
},
searchHTML
;
$module.api(apiSettings);
}
},
query: function() { query: function() {
var var
searchTerm = $prompt.val(), searchTerm = $prompt.val(),
@ -270,15 +285,15 @@ $.fn.search = function(parameters) {
if($.isPlainObject(settings.source) || $.isArray(settings.source)) { if($.isPlainObject(settings.source) || $.isArray(settings.source)) {
module.search.local(searchTerm); module.search.local(searchTerm);
} }
else if(settings.apiSettings) {
module.search.remote(searchTerm);
}
else if($.fn.api !== undefined && $.api.settings.api.search !== undefined) {
module.debug('Searching with default search API endpoint');
settings.apiSettings = {
action: 'search'
};
module.search.remote(searchTerm);
else if($.fn.api !== undefined ) {
if(settings.apiSettings) {
module.debug('Searching with specified API settings', settings.apiSettings);
module.search.remote(searchTerm);
}
else if($.api.settings.api.search !== undefined) {
module.debug('Searching with default search API endpoint');
module.search.remote(searchTerm);
}
} }
else { else {
module.error(error.source); module.error(error.source);
@ -295,8 +310,8 @@ $.fn.search = function(parameters) {
searchFields = $.isArray(settings.searchFields) searchFields = $.isArray(settings.searchFields)
? settings.searchFields ? settings.searchFields
: [settings.searchFields], : [settings.searchFields],
searchRegExp = new RegExp('(?:\s|^)' + searchTerm, 'i'),
fullTextRegExp = new RegExp(searchTerm, 'i'),
searchRegExp = new RegExp('(?:\s|^)' + searchTerm, 'i'),
fullTextRegExp = new RegExp(searchTerm, 'i'),
searchHTML searchHTML
; ;
$module $module
@ -331,25 +346,38 @@ $.fn.search = function(parameters) {
remote: function(searchTerm) { remote: function(searchTerm) {
var var
apiSettings = { apiSettings = {
stateContext : $module,
urlData : {
query: searchTerm
},
onSuccess : function(response) { onSuccess : function(response) {
searchHTML = module.generateResults(response);
module.write.cache(searchTerm, searchHTML);
module.addResults(searchHTML);
module.parse.response.call(element, response, searchTerm);
}, },
onFailure : module.error
},
searchHTML
urlData: {
query: searchTerm
}
}
; ;
module.cancel();
module.debug('Executing search');
if( !$module.api('get request') ) {
module.setup.api();
}
$.extend(true, apiSettings, settings.apiSettings); $.extend(true, apiSettings, settings.apiSettings);
$.api(apiSettings);
module.debug('Executing search', apiSettings);
module.cancel();
$module
.api('setting', apiSettings)
.api('query')
;
} }
},
parse: {
response: function(response, searchTerm) {
var
searchHTML = module.generateResults(response)
;
module.verbose('Parsing server response', response);
if(searchTerm) {
module.write.cache(searchTerm, searchHTML);
}
module.addResults(searchHTML);
}
}, },
throttle: function() { throttle: function() {
@ -387,10 +415,14 @@ $.fn.search = function(parameters) {
var var
cache = $module.data('cache') cache = $module.data('cache')
; ;
return (settings.cache && (typeof cache == 'object') && (cache[name] !== undefined) )
? cache[name]
: false
;
if(settings.cache) {
module.verbose('Checking for server response in cache', name);
return (typeof cache == 'object') && (cache[name] !== undefined)
? cache[name]
: false
;
}
return false;
} }
}, },
@ -401,10 +433,13 @@ $.fn.search = function(parameters) {
? $module.data('cache') ? $module.data('cache')
: {} : {}
; ;
cache[name] = value;
$module
.data('cache', cache)
;
if(settings.cache) {
module.verbose('Writing server response to cache', name, value);
cache[name] = value;
$module
.data('cache', cache)
;
}
} }
}, },
@ -679,7 +714,7 @@ $.fn.search.settings = {
name : 'Search Module', name : 'Search Module',
namespace : 'search', namespace : 'search',
debug : false,
debug : true,
verbose : true, verbose : true,
performance : true, performance : true,

Loading…
Cancel
Save