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

Commit

Permalink
Filterable: fix preventDefault not working
Browse files Browse the repository at this point in the history
Closes gh-7154
Fixes gh-7153

(cherry picked from commit 6b46425)
  • Loading branch information
frequent authored and Gabriel Schulhof committed Apr 8, 2014
1 parent 369eae0 commit 6a08158
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
4 changes: 3 additions & 1 deletion js/widgets/filterable.backcompat.js
Expand Up @@ -72,7 +72,9 @@ $.widget( "mobile.filterable", $.mobile.filterable, {
// Also trigger listviewbeforefilter if this widget is also a listview
this._widget._trigger( "beforefilter", event, data );
}
this._super( type, event, data );

// Passing back the response enables calling preventDefault()
return this._super( type, event, data );
},

_setWidget: function( widget ) {
Expand Down
4 changes: 3 additions & 1 deletion js/widgets/filterable.js
Expand Up @@ -62,7 +62,9 @@ $.widget( "mobile.filterable", {
}

this._timer = this._delay( function() {
this._trigger( "beforefilter", null, { input: search } );
if ( this._trigger( "beforefilter", null, { input: search } ) === false ) {
return false;
}

// Change val as lastval for next execution
search[ 0 ].setAttribute( "data-" + $.mobile.ns + "lastval", val );
Expand Down
23 changes: 23 additions & 0 deletions tests/integration/filterable/filterable_core.js
Expand Up @@ -67,6 +67,29 @@
], 500 );
});

asyncTest( "Filter won't run when preventing default on 'filterablebeforefilter'", function() {
expect( 1 );

var input = $( "#test-prevent-default-handler" ),
listview = $( "#test-prevent-default-signal-emission" );

listview.on( "filterablebeforefilter.theEventIsPrevented", function (e) {
e.preventDefault();
});

$.testHelper.sequence([
function() {
input.val( "a" ).trigger( "change" );
},
function() {
deepEqual( listview.children( ".ui-screen-hidden" ).length, 0,
"No children are hidden." );
listview.off( "filterablebeforefilter.theEventIsPrevented" );
},
start
], 500);
});

asyncTest( "filterCallback and filterReveal can be altered after widget creation", function(){
var filterable = $( "#custom-callback-listview" ),
input = $( "#custom-callback-listview-input" ),
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/filterable/index.html
Expand Up @@ -93,6 +93,17 @@ <h1>Filterable</h1>
<li>d is for darkwing</li>
</ul>

<input id="test-prevent-default-handler"></input>
<ul id="test-prevent-default-signal-emission"
data-nstest-role="listview"
data-nstest-filter="true"
data-nstest-input="#test-prevent-default-handler">
<li>San Francisco, CA</li>
<li>Portland, OR</li>
<li>Washington, DC</li>
<li>New York, NY</li>
</ul>

<input id="custom-callback-listview-input"></input>
<ul id="custom-callback-listview" data-nstest-role="listview" data-nstest-filter="true" data-nstest-input="#custom-callback-listview-input">
<li>a is for aquaman</li>
Expand Down

0 comments on commit 6a08158

Please sign in to comment.