Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit 6b46425

Browse files
frequentGabriel Schulhof
authored and
Gabriel Schulhof
committed
Filterable: fix preventDefault not working
Closes gh-7154 Fixes gh-7153
1 parent 2278fc3 commit 6b46425

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

js/widgets/filterable.backcompat.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ $.widget( "mobile.filterable", $.mobile.filterable, {
7272
// Also trigger listviewbeforefilter if this widget is also a listview
7373
this._widget._trigger( "beforefilter", event, data );
7474
}
75-
this._super( type, event, data );
75+
76+
// Passing back the response enables calling preventDefault()
77+
return this._super( type, event, data );
7678
},
7779

7880
_setWidget: function( widget ) {

js/widgets/filterable.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ $.widget( "mobile.filterable", {
6262
}
6363

6464
this._timer = this._delay( function() {
65-
this._trigger( "beforefilter", null, { input: search } );
65+
if ( this._trigger( "beforefilter", null, { input: search } ) === false ) {
66+
return false;
67+
}
6668

6769
// Change val as lastval for next execution
6870
search[ 0 ].setAttribute( "data-" + $.mobile.ns + "lastval", val );

tests/integration/filterable/filterable_core.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,29 @@
6767
], 500 );
6868
});
6969

70+
asyncTest( "Filter won't run when preventing default on 'filterablebeforefilter'", function() {
71+
expect( 1 );
72+
73+
var input = $( "#test-prevent-default-handler" ),
74+
listview = $( "#test-prevent-default-signal-emission" );
75+
76+
listview.on( "filterablebeforefilter.theEventIsPrevented", function (e) {
77+
e.preventDefault();
78+
});
79+
80+
$.testHelper.sequence([
81+
function() {
82+
input.val( "a" ).trigger( "change" );
83+
},
84+
function() {
85+
deepEqual( listview.children( ".ui-screen-hidden" ).length, 0,
86+
"No children are hidden." );
87+
listview.off( "filterablebeforefilter.theEventIsPrevented" );
88+
},
89+
start
90+
], 500);
91+
});
92+
7093
asyncTest( "filterCallback and filterReveal can be altered after widget creation", function(){
7194
var filterable = $( "#custom-callback-listview" ),
7295
input = $( "#custom-callback-listview-input" ),

tests/integration/filterable/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ <h1>Filterable</h1>
9393
<li>d is for darkwing</li>
9494
</ul>
9595

96+
<input id="test-prevent-default-handler"></input>
97+
<ul id="test-prevent-default-signal-emission"
98+
data-nstest-role="listview"
99+
data-nstest-filter="true"
100+
data-nstest-input="#test-prevent-default-handler">
101+
<li>San Francisco, CA</li>
102+
<li>Portland, OR</li>
103+
<li>Washington, DC</li>
104+
<li>New York, NY</li>
105+
</ul>
106+
96107
<input id="custom-callback-listview-input"></input>
97108
<ul id="custom-callback-listview" data-nstest-role="listview" data-nstest-filter="true" data-nstest-input="#custom-callback-listview-input">
98109
<li>a is for aquaman</li>

0 commit comments

Comments
 (0)