diff --git a/README.textile b/README.textile index 920fb3a..766de18 100644 --- a/README.textile +++ b/README.textile @@ -39,9 +39,10 @@ h2. Options h3. Complete -If you pass a function as a @complete@-option, your function will be executed when the search is complete and the results have been returned, right before they're appended to the page: +If you pass a function as a @complete@-option, your function will be executed when the search is complete and the results have been returned, right before they're appended to the page. Additionally, you can pass a function as a @process@-option that will allow you to make modifications per result (the following example truncates each result to 50 words): bc. $('#search_results').tapir({ 'token': '4dbfc79e3f61b05b53000021', - 'complete' : function() { alert("I'm done searching!"); } + 'complete' : function() { alert("I'm done searching!"); }, + 'process' : function(result) { result.summary = result.summary.split(' ', 50).join(' ') + '...'; return result; } }); diff --git a/jquery-tapir.js b/jquery-tapir.js index e041a45..8cbcae0 100644 --- a/jquery-tapir.js +++ b/jquery-tapir.js @@ -22,7 +22,14 @@ $.getJSON( 'http://tapirgo.com/api/1/search.json?token=' + settings.token + '&query=' + paramValue(settings.query_param) + '&callback=?', function(data){ if(settings['complete']) { settings.complete() } + + // Clear div if we have results + if(data.length > 0) + el.empty(); + $.each(data, function(key, val) { + if(settings['process']) + val = settings.process(val); el.append('

' + val.title + '

' + val.summary + '

'); }); } diff --git a/jquery-tapir.min.js b/jquery-tapir.min.js index 1b4cbdd..7df9d92 100644 --- a/jquery-tapir.min.js +++ b/jquery-tapir.min.js @@ -1,5 +1 @@ -(function($){var el;var settings={};var methods={init:function(options){el=this;settings={token:false,query_param:'query'};if(options){$.extend(settings,options);} -if(!settings.token||settings.query_param==''){return this;} -$.getJSON('http://tapirgo.com/api/1/search.json?token='+settings.token+'&query='+paramValue(settings.query_param)+'&callback=?',function(data){if(settings['complete']){settings.complete()} -$.each(data,function(key,val){el.append('

'+val.title+'

'+val.summary+'

');});});return this;}};function paramValue(query_param){var results=new RegExp('[\\?&]'+query_param+'=([^&#]*)').exec(window.location.href);return results?results[1]:false;} -$.fn.tapir=function(method){if(methods[method]){return methods[method].apply(this,Array.prototype.slice.call(arguments,1));}else if(typeof method==='object'||!method){return methods.init.apply(this,arguments);}else{$.error('Method '+method+' does not exist on jQuery.tapir');}};})(jQuery); \ No newline at end of file +(function(d){var c;var b={};var a={init:function(f){c=this;b={token:false,query_param:"query"};if(f){d.extend(b,f)}if(!b.token||b.query_param==""){return this}d.getJSON("http://tapirgo.com/api/1/search.json?token="+b.token+"&query="+e(b.query_param)+"&callback=?",function(g){if(b.complete){b.complete()}if(g.length>0){c.empty()}d.each(g,function(h,i){if(b.process){i=b.process(i)}c.append('

'+i.title+"

"+i.summary+"

")})});return this}};function e(f){var g=new RegExp("[\\?&]"+f+"=([^&#]*)").exec(window.location.href);return g?g[1]:false}d.fn.tapir=function(f){if(a[f]){return a[f].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof f==="object"||!f){return a.init.apply(this,arguments)}else{d.error("Method "+f+" does not exist on jQuery.tapir")}}}})(jQuery); \ No newline at end of file