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

Commit

Permalink
Listview and extensions: Simplifying extensibility with the introduct…
Browse files Browse the repository at this point in the history
…ion of the private methods _beforeListviewRefresh and _afterListviewRefresh that surround the listview refresh and can be overridden from extensions.

Listview autodividers: Use .children() instead of .find() to avoid affecting nested lists.
Performance improvement: http://jsperf.com/improve-autodividers
  • Loading branch information
Gabriel Schulhof committed Aug 26, 2013
1 parent 614d5e3 commit b4cb217
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
25 changes: 7 additions & 18 deletions js/widgets/listview.autodividers.js
Expand Up @@ -26,12 +26,11 @@ $.widget( "mobile.listview", $.mobile.listview, {
autodividersSelector: defaultAutodividersSelector
},

_afterListviewRefresh: function() {
var el = this.element;
this._off( el, "listviewafterrefresh" );
this._replaceDividers();
this.refresh();
this._on( el, { listviewafterrefresh: "_afterListviewRefresh" } );
_beforeListviewRefresh: function() {
if ( this.options.autodividers ) {
this._replaceDividers();
this._superApply( arguments );
}
},

_replaceDividers: function() {
Expand All @@ -40,9 +39,9 @@ $.widget( "mobile.listview", $.mobile.listview, {
list = this.element,
divider;

list.find( "li:jqmData(role='list-divider')" ).remove();
list.children( "li:jqmData(role='list-divider')" ).remove();

lis = list.find( "li" );
lis = list.children( "li" );

for ( i = 0; i < lis.length ; i++ ) {
li = lis[ i ];
Expand All @@ -57,16 +56,6 @@ $.widget( "mobile.listview", $.mobile.listview, {

lastDividerText = dividerText;
}
},

_create: function() {
this._super();

if ( !this.options.autodividers ) {
return;
}

this._afterListviewRefresh();
}
});

Expand Down
2 changes: 1 addition & 1 deletion js/widgets/listview.hidedividers.js
Expand Up @@ -14,7 +14,7 @@ $.widget( "mobile.listview", $.mobile.listview, {
hidedividers: false
},

refresh: function() {
_afterListviewRefresh: function() {
var items, idx, item, hideDivider = true;

this._superApply( arguments );
Expand Down
14 changes: 10 additions & 4 deletions js/widgets/listview.js
Expand Up @@ -79,13 +79,15 @@ $.widget( "mobile.listview", $.extend( {
return $( results );
},

_beforeListviewRefresh: $.noop,
_afterListviewRefresh: $.noop,

refresh: function( create ) {
var buttonClass, pos, numli, item, itemClass, itemTheme, itemIcon, icon, a,
isDivider, startCount, newStartCount, value, last, splittheme, splitThemeClass, spliticon,
altButtonClass, dividerTheme,
altButtonClass, dividerTheme, li,
o = this.options,
$list = this.element,
li = this._getChildrenByTagName( $list[ 0 ], "li", "LI" ),
ol = !!$.nodeName( $list[ 0 ], "ol" ),
start = $list.attr( "start" ),
itemClassDict = {},
Expand All @@ -103,6 +105,10 @@ $.widget( "mobile.listview", $.extend( {
$list.css( "counter-reset", "listnumbering " + startCount );
}

this._beforeListviewRefresh();

li = this._getChildrenByTagName( $list[ 0 ], "li", "LI" );

for ( pos = 0, numli = li.length; pos < numli; pos++ ) {
item = li.eq( pos );
itemClass = "";
Expand Down Expand Up @@ -193,9 +199,9 @@ $.widget( "mobile.listview", $.extend( {
this._addThumbClasses( li );
this._addThumbClasses( li.find( ".ui-btn" ) );

this._afterListviewRefresh();

this._addFirstLastClasses( li, this._getVisibles( li, create ), create );
// autodividers binds to this to redraw dividers after the listview refresh
this._trigger( "afterrefresh" );
}
}, $.mobile.behaviors.addFirstLastClasses ) );

Expand Down

0 comments on commit b4cb217

Please sign in to comment.