Cache the region filter results

Change-Id: Ia303774d3c1171f3e8c1b3f7ca8b102c79e68300
This commit is contained in:
Santhosh Thottingal
2012-08-21 17:06:41 +05:30
parent 01ceb4c4f8
commit fbb55dfe6f

View File

@@ -33,6 +33,7 @@
this.options = $.extend( {}, $.fn.regionselector.defaults, options ); this.options = $.extend( {}, $.fn.regionselector.defaults, options );
this.$element.addClass( 'regionselector' ); this.$element.addClass( 'regionselector' );
this.regions = []; this.regions = [];
this.cache= null;
this.regionGroup = this.$element.data( 'regiongroup' ); this.regionGroup = this.$element.data( 'regiongroup' );
this.init(); this.init();
this.listen(); this.listen();
@@ -57,6 +58,7 @@
region = this.regions[i]; region = this.regions[i];
if ( $.inArray( region, langRegions ) >= 0 ) { if ( $.inArray( region, langRegions ) >= 0 ) {
this.render( langCode, region ); this.render( langCode, region );
this.cache[langCode] = region;
return; return;
} }
} }
@@ -69,15 +71,28 @@
// if there is a region group, make it active. // if there is a region group, make it active.
this.$element.addClass( 'active' ); this.$element.addClass( 'active' );
} }
// Re-populate the list of languages // Re-populate the list of languages
this.options.$target.empty(); this.options.$target.empty();
var languagesByScriptGroup = $.uls.data.languagesByScriptGroup( this.options.languages );
for ( var scriptGroup in languagesByScriptGroup ) { if ( this.cache ) {
var languages = languagesByScriptGroup[scriptGroup]; // If the result cache is present, render the results from there.
languages.sort( $.uls.data.sortByAutonym ); var result = null;
for ( var i = 0; i < languages.length; i++ ) { for ( result in this.cache ) {
this.test( languages[i] ); this.render( result, this.cache[result] );
}
} else {
this.cache = {};
// Get the languages grouped by script group
var languagesByScriptGroup = $.uls.data.languagesByScriptGroup( this.options.languages );
for ( var scriptGroup in languagesByScriptGroup ) {
// Get the languages for the script group
var languages = languagesByScriptGroup[scriptGroup];
// Sort it based on autonym
languages.sort( $.uls.data.sortByAutonym );
for ( var i = 0; i < languages.length; i++ ) {
// Check whether it belongs to the region
this.test( languages[i] );
}
} }
} }