Cache the $(this.form_field) call for better performance. Thanks, pomeh.

abstract-chosen
Patrick Filler 2011-07-20 10:41:07 -04:00
parent 54ac37ccf5
commit a3c9ea9a29
2 changed files with 20 additions and 19 deletions

View File

@ -21,6 +21,7 @@
function Chosen(elmn) { function Chosen(elmn) {
this.set_default_values(); this.set_default_values();
this.form_field = elmn; this.form_field = elmn;
this.form_field_jq = $(this.form_field);
this.is_multiple = this.form_field.multiple; this.is_multiple = this.form_field.multiple;
this.default_text_default = this.form_field.multiple ? "Select Some Options" : "Select an Option"; this.default_text_default = this.form_field.multiple ? "Select Some Options" : "Select an Option";
this.set_up_html(); this.set_up_html();
@ -40,8 +41,8 @@
Chosen.prototype.set_up_html = function() { Chosen.prototype.set_up_html = function() {
var container_div, dd_top, dd_width, sf_width; var container_div, dd_top, dd_width, sf_width;
this.container_id = this.form_field.id + "_chzn"; this.container_id = this.form_field.id + "_chzn";
this.f_width = ($(this.form_field)).width(); this.f_width = this.form_field_jq.width();
this.default_text = ($(this.form_field)).attr('title') ? ($(this.form_field)).attr('title') : this.default_text_default; this.default_text = this.form_field_jq.attr('title') ? this.form_field_jq.attr('title') : this.default_text_default;
container_div = $("<div />", { container_div = $("<div />", {
id: this.container_id, id: this.container_id,
"class": 'chzn-container', "class": 'chzn-container',
@ -52,7 +53,7 @@
} else { } else {
container_div.html('<a href="#" class="chzn-single"><span>' + this.default_text + '</span><div><b></b></div></a><div class="chzn-drop" style="left:-9000px;"><div class="chzn-search"><input type="text" /></div><ul class="chzn-results"></ul></div>'); container_div.html('<a href="#" class="chzn-single"><span>' + this.default_text + '</span><div><b></b></div></a><div class="chzn-drop" style="left:-9000px;"><div class="chzn-search"><input type="text" /></div><ul class="chzn-results"></ul></div>');
} }
($(this.form_field)).hide().after(container_div); this.form_field_jq.hide().after(container_div);
this.container = $('#' + this.container_id); this.container = $('#' + this.container_id);
this.container.addClass("chzn-container-" + (this.is_multiple ? "multi" : "single")); this.container.addClass("chzn-container-" + (this.is_multiple ? "multi" : "single"));
this.dropdown = this.container.find('div.chzn-drop').first(); this.dropdown = this.container.find('div.chzn-drop').first();
@ -99,7 +100,7 @@
this.search_results.mouseout(__bind(function(evt) { this.search_results.mouseout(__bind(function(evt) {
return this.search_results_mouseout(evt); return this.search_results_mouseout(evt);
}, this)); }, this));
($(this.form_field)).bind("liszt:updated", __bind(function(evt) { this.form_field_jq.bind("liszt:updated", __bind(function(evt) {
return this.results_update_field(evt); return this.results_update_field(evt);
}, this)); }, this));
this.search_field.blur(__bind(function(evt) { this.search_field.blur(__bind(function(evt) {
@ -318,9 +319,9 @@
}; };
Chosen.prototype.set_tab_index = function(el) { Chosen.prototype.set_tab_index = function(el) {
var ti; var ti;
if (($(this.form_field)).attr("tabindex")) { if (this.form_field_jq.attr("tabindex")) {
ti = ($(this.form_field)).attr("tabindex"); ti = this.form_field_jq.attr("tabindex");
($(this.form_field)).attr("tabindex", -1); this.form_field_jq.attr("tabindex", -1);
if (this.is_multiple) { if (this.is_multiple) {
return this.search_field.attr("tabindex", ti); return this.search_field.attr("tabindex", ti);
} else { } else {
@ -411,7 +412,7 @@
} }
this.results_hide(); this.results_hide();
this.search_field.val(""); this.search_field.val("");
($(this.form_field)).trigger("change"); this.form_field_jq.trigger("change");
return this.search_field_scale(); return this.search_field_scale();
} }
}; };
@ -430,7 +431,7 @@
result.removeClass("result-selected").addClass("active-result").show(); result.removeClass("result-selected").addClass("active-result").show();
this.result_clear_highlight(); this.result_clear_highlight();
this.winnow_results(); this.winnow_results();
($(this.form_field)).trigger("change"); this.form_field_jq.trigger("change");
return this.search_field_scale(); return this.search_field_scale();
}; };
Chosen.prototype.results_search = function(evt) { Chosen.prototype.results_search = function(evt) {

View File

@ -23,6 +23,7 @@ class Chosen
this.set_default_values() this.set_default_values()
@form_field = elmn @form_field = elmn
@form_field_jq = $ @form_field
@is_multiple = @form_field.multiple @is_multiple = @form_field.multiple
@default_text_default = if @form_field.multiple then "Select Some Options" else "Select an Option" @default_text_default = if @form_field.multiple then "Select Some Options" else "Select an Option"
@ -30,7 +31,6 @@ class Chosen
this.set_up_html() this.set_up_html()
this.register_observers() this.register_observers()
set_default_values: -> set_default_values: ->
@click_test_action = (evt) => this.test_active_click(evt) @click_test_action = (evt) => this.test_active_click(evt)
@ -44,9 +44,9 @@ class Chosen
set_up_html: -> set_up_html: ->
@container_id = @form_field.id + "_chzn" @container_id = @form_field.id + "_chzn"
@f_width = ($ @form_field).width() @f_width = @form_field_jq.width()
@default_text = if ($ @form_field).attr 'title' then ($ @form_field).attr 'title' else @default_text_default @default_text = if @form_field_jq.attr 'title' then @form_field_jq.attr 'title' else @default_text_default
container_div = ($ "<div />", { container_div = ($ "<div />", {
id: @container_id id: @container_id
@ -59,7 +59,7 @@ class Chosen
else else
container_div.html '<a href="#" class="chzn-single"><span>' + @default_text + '</span><div><b></b></div></a><div class="chzn-drop" style="left:-9000px;"><div class="chzn-search"><input type="text" /></div><ul class="chzn-results"></ul></div>'; container_div.html '<a href="#" class="chzn-single"><span>' + @default_text + '</span><div><b></b></div></a><div class="chzn-drop" style="left:-9000px;"><div class="chzn-search"><input type="text" /></div><ul class="chzn-results"></ul></div>';
($ @form_field).hide().after container_div @form_field_jq.hide().after container_div
@container = ($ '#' + @container_id) @container = ($ '#' + @container_id)
@container.addClass( "chzn-container-" + (if @is_multiple then "multi" else "single") ) @container.addClass( "chzn-container-" + (if @is_multiple then "multi" else "single") )
@dropdown = @container.find('div.chzn-drop').first() @dropdown = @container.find('div.chzn-drop').first()
@ -97,7 +97,7 @@ class Chosen
@search_results.mouseover (evt) => this.search_results_mouseover(evt) @search_results.mouseover (evt) => this.search_results_mouseover(evt)
@search_results.mouseout (evt) => this.search_results_mouseout(evt) @search_results.mouseout (evt) => this.search_results_mouseout(evt)
($ @form_field).bind "liszt:updated", (evt) => this.results_update_field(evt) @form_field_jq.bind "liszt:updated", (evt) => this.results_update_field(evt)
@search_field.blur (evt) => this.input_blur(evt) @search_field.blur (evt) => this.input_blur(evt)
@search_field.keyup (evt) => this.keyup_checker(evt) @search_field.keyup (evt) => this.keyup_checker(evt)
@ -279,9 +279,9 @@ class Chosen
set_tab_index: (el) -> set_tab_index: (el) ->
if ($ @form_field).attr "tabindex" if @form_field_jq.attr "tabindex"
ti = ($ @form_field).attr "tabindex" ti = @form_field_jq.attr "tabindex"
($ @form_field).attr "tabindex", -1 @form_field_jq.attr "tabindex", -1
if @is_multiple if @is_multiple
@search_field.attr "tabindex", ti @search_field.attr "tabindex", ti
@ -365,7 +365,7 @@ class Chosen
this.results_hide() this.results_hide()
@search_field.val "" @search_field.val ""
($ @form_field).trigger "change" @form_field_jq.trigger "change"
this.search_field_scale() this.search_field_scale()
result_activate: (el) -> result_activate: (el) ->
@ -385,7 +385,7 @@ class Chosen
this.result_clear_highlight() this.result_clear_highlight()
this.winnow_results() this.winnow_results()
($ @form_field).trigger "change" @form_field_jq.trigger "change"
this.search_field_scale() this.search_field_scale()
results_search: (evt) -> results_search: (evt) ->