Skip to content

Commit ec88ea2

Browse files
committed
Merge pull request jquery-archive#5419 from gseguin/issue-5198
Fixes jquery-archive#5198
2 parents 8c3c008 + 88e86a1 commit ec88ea2

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

js/widgets/listview.filter.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,7 @@ $.mobile.document.delegate( "ul, ol", "listviewcreate", function() {
3939
e.preventDefault();
4040
search.blur();
4141
}),
42-
search = $( "<input>", {
43-
placeholder: listview.options.filterPlaceholder
44-
})
45-
.attr( "data-" + $.mobile.ns + "type", "search" )
46-
.jqmData( "lastval", "" )
47-
.bind( "keyup change input", function() {
48-
42+
onKeyUp = function( e ) {
4943
var $this = $( this ),
5044
val = this.value.toLowerCase(),
5145
listItems = null,
@@ -57,6 +51,11 @@ $.mobile.document.delegate( "ul, ol", "listviewcreate", function() {
5751
// Check if a custom filter callback applies
5852
isCustomFilterCallback = listview.options.filterCallback !== defaultFilterCallback;
5953

54+
if ( lastval && lastval === val ) {
55+
// Execute the handler only once per value change
56+
return;
57+
}
58+
6059
listview._trigger( "beforefilter", "beforefilter", { input: this } );
6160

6261
// Change val as lastval for next execution
@@ -119,7 +118,13 @@ $.mobile.document.delegate( "ul, ol", "listviewcreate", function() {
119118
listItems.toggleClass( "ui-screen-hidden", !!listview.options.filterReveal );
120119
}
121120
listview._addFirstLastClasses( li, listview._getVisibles( li, false ), false );
121+
},
122+
search = $( "<input>", {
123+
placeholder: listview.options.filterPlaceholder
122124
})
125+
.attr( "data-" + $.mobile.ns + "type", "search" )
126+
.jqmData( "lastval", "" )
127+
.bind( "keyup change input", onKeyUp )
123128
.appendTo( wrapper )
124129
.textinput();
125130

tests/unit/listview/listview_core.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,34 @@
467467
]);
468468
});
469469

470+
asyncTest( "event listviewbeforefilter firing", function() {
471+
var $searchPage = $( searchFilterId );
472+
$.testHelper.pageSequence([
473+
function() {
474+
$.mobile.changePage( searchFilterId );
475+
},
476+
477+
function() {
478+
var beforeFilterCount = 0;
479+
$searchPage.on( "listviewbeforefilter", function( e ) {
480+
beforeFilterCount += 1;
481+
});
482+
$searchPage.find( 'input' ).val( "a" );
483+
$searchPage.find( 'input' ).trigger('input');
484+
$searchPage.find( 'input' ).trigger('keyup');
485+
$searchPage.find( 'input' ).trigger('change');
486+
equal( beforeFilterCount, 1, "listviewbeforefilter should fire only once for the same value" );
487+
488+
$searchPage.find( 'input' ).val( "ab" );
489+
$searchPage.find( 'input' ).trigger('input');
490+
$searchPage.find( 'input' ).trigger('keyup');
491+
equal( beforeFilterCount, 2, "listviewbeforefilter should fire twice since value has changed" );
492+
493+
start();
494+
}
495+
]);
496+
});
497+
470498
test( "Refresh applies thumb styling", function(){
471499
var ul = $('.ui-page-active ul');
472500

0 commit comments

Comments
 (0)