diff --git a/README.md b/README.md index 33c33517..095019c1 100644 --- a/README.md +++ b/README.md @@ -210,6 +210,22 @@ NOTE: Setting the `:multiple` option to `true` will result in the chosen values Now your autocomplete code is unobtrusive, Rails 3 style. +#### Client-side config + +To configure the behaviour if no matches are found, you can set the following options: + + jQuery.railsAutocomplete.options.showNoMatches //default 'true' (yeah, it's a string) + jQuery.railsAutocomplete.options.noMatchesLabel //default 'no existing matches' + +These will change the behaviour globally. To set them on a single input field use: + + f.autocomplete_field :brand_names, autocomplete_brand_name_products_path, + 'data-showNoMatches' => 'false' + #or + f.autocomplete_field :brand_names, autocomplete_brand_name_products_path, + 'data-noMatchesLabel' => 'no brands found' + + ### Getting the object id If you need to use the id of the selected object, you can use the *id_element* attribute too: diff --git a/lib/assets/javascripts/autocomplete-rails-uncompressed.js b/lib/assets/javascripts/autocomplete-rails-uncompressed.js index 1a4d0837..533f06b4 100644 --- a/lib/assets/javascripts/autocomplete-rails-uncompressed.js +++ b/lib/assets/javascripts/autocomplete-rails-uncompressed.js @@ -35,6 +35,10 @@ _e = e; this.init(_e); }; + jQuery.railsAutocomplete.options = { + showNoMatches: 'true', + noMatchesLabel: 'no existing match' + } jQuery.railsAutocomplete.fn = jQuery.railsAutocomplete.prototype = { railsAutocomplete: '0.0.1' @@ -56,9 +60,17 @@ jQuery.getJSON( jQuery(e).attr('data-autocomplete'), { term: extractLast( request.term ) }, function() { - if(arguments[0].length == 0) { + var options = {} + jQuery.extend(options, jQuery.railsAutocomplete.options); + jQuery.each(options, function(key, value) { + if(options.hasOwnProperty(key)) { + var attr_val = jQuery(e).attr('data-' + key); + options[key] = attr_val ? attr_val : value; + } + }); + if(arguments[0].length == 0 && options.showNoMatches == 'true') { arguments[0] = [] - arguments[0][0] = { id: "", label: "no existing match" } + arguments[0][0] = { id: "", label: options.noMatchesLabel } } jQuery(arguments[0]).each(function(i, el) { var obj = {}; diff --git a/lib/assets/javascripts/autocomplete-rails.js b/lib/assets/javascripts/autocomplete-rails.js index 6e173e74..8f15140d 100644 --- a/lib/assets/javascripts/autocomplete-rails.js +++ b/lib/assets/javascripts/autocomplete-rails.js @@ -1,16 +1 @@ -/* -* Unobtrusive autocomplete -* -* To use it, you just have to include the HTML attribute autocomplete -* with the autocomplete URL as the value -* -* Example: -* -* -* Optionally, you can use a jQuery selector to specify a field that can -* be updated with the element id whenever you find a matching value -* -* Example: -* -*/ -(function(e){var t=null;e.fn.railsAutocomplete=function(){var t=function(){this.railsAutoCompleter||(this.railsAutoCompleter=new e.railsAutocomplete(this))};return e.fn.on!==undefined?$(document).on("focus",this.selector,t):this.live("focus",t)},e.railsAutocomplete=function(e){_e=e,this.init(_e)},e.railsAutocomplete.fn=e.railsAutocomplete.prototype={railsAutocomplete:"0.0.1"},e.railsAutocomplete.fn.extend=e.railsAutocomplete.extend=e.extend,e.railsAutocomplete.fn.extend({init:function(t){function n(e){return e.split(t.delimiter)}function r(e){return n(e).pop().replace(/^\s+/,"")}t.delimiter=e(t).attr("data-delimiter")||null,e(t).autocomplete({source:function(n,i){e.getJSON(e(t).attr("data-autocomplete"),{term:r(n.term)},function(){arguments[0].length==0&&(arguments[0]=[],arguments[0][0]={id:"",label:"no existing match"}),e(arguments[0]).each(function(n,r){var i={};i[r.id]=r,e(t).data(i)}),i.apply(null,arguments)})},change:function(t,n){if(e(e(this).attr("data-id-element")).val()=="")return;e(e(this).attr("data-id-element")).val(n.item?n.item.id:"");var r=e.parseJSON(e(this).attr("data-update-elements")),i=n.item?e(this).data(n.item.id.toString()):{};if(r&&e(r["id"]).val()=="")return;for(var s in r)e(r[s]).val(n.item?i[s]:"")},search:function(){var e=r(this.value);if(e.length<2)return!1},focus:function(){return!1},select:function(r,i){var s=n(this.value);s.pop(),s.push(i.item.value);if(t.delimiter!=null)s.push(""),this.value=s.join(t.delimiter);else{this.value=s.join(""),e(this).attr("data-id-element")&&e(e(this).attr("data-id-element")).val(i.item.id);if(e(this).attr("data-update-elements")){var o=e(this).data(i.item.id.toString()),u=e.parseJSON(e(this).attr("data-update-elements"));for(var a in u)e(u[a]).val(o[a])}}var f=this.value;return e(this).bind("keyup.clearId",function(){e(this).val().trim()!=f.trim()&&(e(e(this).attr("data-id-element")).val(""),e(this).unbind("keyup.clearId"))}),e(t).trigger("railsAutocomplete.select",i),!1}})}}),e(document).ready(function(){e("input[data-autocomplete]").railsAutocomplete()})})(jQuery); \ No newline at end of file +!function(t){t.fn.railsAutocomplete=function(){var e=function(){this.railsAutoCompleter||(this.railsAutoCompleter=new t.railsAutocomplete(this))};return void 0!==t.fn.on?$(document).on("focus",this.selector,e):this.live("focus",e)},t.railsAutocomplete=function(t){_e=t,this.init(_e)},t.railsAutocomplete.options={showNoMatches:"true",noMatchesLabel:"no existing match"},t.railsAutocomplete.fn=t.railsAutocomplete.prototype={railsAutocomplete:"0.0.1"},t.railsAutocomplete.fn.extend=t.railsAutocomplete.extend=t.extend,t.railsAutocomplete.fn.extend({init:function(e){function i(t){return t.split(e.delimiter)}function a(t){return i(t).pop().replace(/^\s+/,"")}e.delimiter=t(e).attr("data-delimiter")||null,t(e).autocomplete({source:function(i,n){t.getJSON(t(e).attr("data-autocomplete"),{term:a(i.term)},function(){var i={};t.extend(i,t.railsAutocomplete.options),t.each(i,function(a,n){if(i.hasOwnProperty(a)){var o=t(e).attr("data-"+a);i[a]=o?o:n}}),0==arguments[0].length&&"true"==i.showNoMatches&&(arguments[0]=[],arguments[0][0]={id:"",label:i.noMatchesLabel}),t(arguments[0]).each(function(i,a){var n={};n[a.id]=a,t(e).data(n)}),n.apply(null,arguments)})},change:function(e,i){if(""!=t(t(this).attr("data-id-element")).val()){t(t(this).attr("data-id-element")).val(i.item?i.item.id:"");var a=t.parseJSON(t(this).attr("data-update-elements")),n=i.item?t(this).data(i.item.id.toString()):{};if(!a||""!=t(a.id).val())for(var o in a)t(a[o]).val(i.item?n[o]:"")}},search:function(){var t=a(this.value);return t.length<2?!1:void 0},focus:function(){return!1},select:function(a,n){var o=i(this.value);if(o.pop(),o.push(n.item.value),null!=e.delimiter)o.push(""),this.value=o.join(e.delimiter);else if(this.value=o.join(""),t(this).attr("data-id-element")&&t(t(this).attr("data-id-element")).val(n.item.id),t(this).attr("data-update-elements")){var r=t(this).data(n.item.id.toString()),l=t.parseJSON(t(this).attr("data-update-elements"));for(var u in l)t(l[u]).val(r[u])}var s=this.value;return t(this).bind("keyup.clearId",function(){t(this).val().trim()!=s.trim()&&(t(t(this).attr("data-id-element")).val(""),t(this).unbind("keyup.clearId"))}),t(e).trigger("railsAutocomplete.select",n),!1}})}}),t(document).ready(function(){t("input[data-autocomplete]").railsAutocomplete()})}(jQuery); \ No newline at end of file diff --git a/lib/rails3-jquery-autocomplete/version.rb b/lib/rails3-jquery-autocomplete/version.rb index 639277d4..fed3ebdd 100644 --- a/lib/rails3-jquery-autocomplete/version.rb +++ b/lib/rails3-jquery-autocomplete/version.rb @@ -1,3 +1,3 @@ module Rails3JQueryAutocomplete - VERSION = '1.0.11' + VERSION = '1.0.13' end