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

Commit 4dbe745

Browse files
author
Gabriel Schulhof
committed
Listview: Re-implement recursion limitation
Closes gh-7646 Fixes gh-7644 Re gh-5680
1 parent 64d71a6 commit 4dbe745

File tree

3 files changed

+70
-20
lines changed

3 files changed

+70
-20
lines changed

js/widgets/filterable.backcompat.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
define( [
88
"jquery",
9-
"./listview",
9+
"./listview.hidedividers",
1010
"./filterable" ], function( jQuery ) {
1111
//>>excludeEnd("jqmBuildExclude");
1212
(function( $, undefined ) {
@@ -160,6 +160,24 @@ $.widget( "mobile.filterable", $.mobile.filterable, {
160160
return ret;
161161
},
162162

163+
// The listview implementation accompanying this filterable backcompat layer will call
164+
// filterable.refresh() after it's done refreshing the listview to make sure the filterable
165+
// filters out any new items added. However, when the listview refresh has been initiated by
166+
// the filterable itself, then such filtering has already taken place, and calling the
167+
// filterable's refresh() method will cause an infinite recursion. We stop this by setting a
168+
// flag that will cause the filterable's refresh() method to short-circuit.
169+
_refreshChildWidget: function() {
170+
this._refreshingChildWidget = true;
171+
this._superApply( arguments );
172+
this._refreshingChildWidget = false;
173+
},
174+
175+
refresh: function() {
176+
if ( !this._refreshingChildWidget ) {
177+
this._superApply( arguments );
178+
}
179+
},
180+
163181
_destroy: function() {
164182
if ( this._isSearchInternal() ) {
165183
this._search.remove();
@@ -207,29 +225,18 @@ $.widget( "mobile.listview", $.mobile.listview, {
207225
return this._super();
208226
},
209227

210-
_afterListviewRefresh: function() {
211-
var filterable = this.element.data( "mobile-filterable" );
212-
213-
if ( this.options.filter === true && filterable ) {
214-
this._preventRefreshLoop = true;
215-
filterable.refresh();
216-
}
217-
},
218-
219-
// Eliminate infinite recursion caused by the fact that we call filterable.refresh() from
220-
// _afterListviewRefresh() above, which, in turn, calls _refreshChildWidget(), which, in
221-
// turn, calls listview refresh(), which would, in turn, calls _afterListviewRefresh()
222-
// above, if we wouldn't prevent that right here.
223228
refresh: function() {
224-
var returnValue;
229+
var filterable;
225230

226-
if ( !this._preventRefreshLoop ) {
227-
returnValue = this._superApply( arguments );
228-
}
231+
this._superApply( arguments );
229232

230-
this._preventRefreshLoop = false;
233+
if ( this.options.filter === true ) {
234+
filterable = this.element.data( "mobile-filterable" );
231235

232-
return returnValue;
236+
if ( filterable ) {
237+
filterable.refresh();
238+
}
239+
}
233240
}
234241
});
235242

tests/unit/filterable/filterable_core.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,28 @@ test( "Refreshing the listview also refreshes the filterable", function() {
7171
"All list items are hidden after listview refresh" );
7272
});
7373

74+
asyncTest( "Empty dividers are hidden by default", function() {
75+
var input = $( "#hide-empty-dividers-input" ),
76+
listview = $( "#hide-empty-dividers" );
77+
78+
$.testHelper.detailedEventCascade([
79+
function() {
80+
input.val( "l" ).trigger( "change" );
81+
},
82+
{
83+
filterablefilter: {
84+
src: listview,
85+
event: "filterablebeforefilter.emptyDividersHidden1"
86+
}
87+
},
88+
function( result ) {
89+
deepEqual( result.filterablefilter.timedOut, false,
90+
"filterablefilter event was triggered" );
91+
deepEqual( $( "#prev-is-hidden" ).prev().hasClass( "ui-screen-hidden" ), true,
92+
"Divider for hidden item is hidden" );
93+
start();
94+
}
95+
]);
96+
});
97+
7498
})( jQuery );

tests/unit/filterable/index.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,25 @@
7070
<li>Item 1</li>
7171
<li>Item 2</li>
7272
</ul>
73+
<form class="ui-filterable">
74+
<input id="hide-empty-dividers-input" data-nstest-type="search">
75+
</form>
76+
<ul id="hide-empty-dividers" data-nstest-role="listview" data-nstest-filter="true" data-nstest-input="#hide-empty-dividers-input" data-nstest-autodividers="true" data-nstest-inset="true">
77+
<li><a href="#">Adele</a></li>
78+
<li><a href="#">Agnes</a></li>
79+
<li><a href="#">Albert</a></li>
80+
<li><a href="#">Billy</a></li>
81+
<li><a href="#">Bob</a></li>
82+
<li><a href="#">Calvin</a></li>
83+
<li><a href="#">Cameron</a></li>
84+
<li><a href="#">Chloe</a></li>
85+
<li><a href="#">Christina</a></li>
86+
<li id="prev-is-hidden"><a href="#">Diana</a></li>
87+
<li><a href="#">Gabriel</a></li>
88+
<li><a href="#">Glen</a></li>
89+
<li><a href="#">Ralph</a></li>
90+
<li><a href="#">Valarie</a></li>
91+
</ul>
7392
</div>
7493
</div>
7594
</body>

0 commit comments

Comments
 (0)