Skip to content

Commit 86e4e6f

Browse files
committed
Some code optimizations to speed up page enhancement.
- Avoid using $.each() when you can directly iterate on the collection. This avoids extra function calling overhead. - Avoid calling jqmData() for options that are specified. - Avoid calling $.fn.wrapInner(), creating the DOM nodes manually is much faster.
1 parent 7057111 commit 86e4e6f

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

js/jquery.mobile.buttonMarkup.js

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,30 @@
77
( function( $, undefined ) {
88

99
$.fn.buttonMarkup = function( options ) {
10-
return this.each( function() {
11-
var el = $( this ),
10+
options = options || {};
11+
12+
for ( var i = 0; i < this.length; i++ ) {
13+
var el = this.eq( i ),
14+
e = el[ 0 ],
1215
o = $.extend( {}, $.fn.buttonMarkup.defaults, {
13-
icon: el.jqmData( "icon" ),
14-
iconpos: el.jqmData( "iconpos" ),
15-
theme: el.jqmData( "theme" ),
16-
inline: el.jqmData( "inline" ),
17-
shadow: el.jqmData( "shadow" ),
18-
corners: el.jqmData( "corners" ),
19-
iconshadow: el.jqmData( "iconshadow" )
16+
icon: options.icon || el.jqmData( "icon" ),
17+
iconpos: options.iconpos || el.jqmData( "iconpos" ),
18+
theme: options.theme || el.jqmData( "theme" ),
19+
inline: options.inline || el.jqmData( "inline" ),
20+
shadow: options.shadow || el.jqmData( "shadow" ),
21+
corners: options.corners || el.jqmData( "corners" ),
22+
iconshadow: options.iconshadow || el.jqmData( "iconshadow" )
2023
}, options ),
2124

2225
// Classes Defined
2326
innerClass = "ui-btn-inner",
2427
textClass = "ui-btn-text",
2528
buttonClass, iconClass,
26-
wrap;
29+
30+
// Button inner markup
31+
buttonInner = document.createElement( o.wrapperEls ),
32+
buttonText = document.createElement( o.wrapperEls ),
33+
buttonIcon = o.icon ? document.createElement( "span" ) : null;
2734

2835
if ( attachEvents ) {
2936
attachEvents();
@@ -71,17 +78,30 @@ $.fn.buttonMarkup = function( options ) {
7178
el.attr( "data-" + $.mobile.ns + "theme", o.theme )
7279
.addClass( buttonClass );
7380

74-
wrap = ( "<D class='" + innerClass + "' aria-hidden='true'><D class='" + textClass + "'></D>" +
75-
( o.icon ? "<span class='" + iconClass + "'></span>" : "" ) +
76-
"</D>" ).replace( /D/g, o.wrapperEls );
81+
buttonInner.className = innerClass;
82+
buttonInner.setAttribute("aria-hidden", "true");
83+
84+
buttonText.className = textClass;
85+
buttonInner.appendChild( buttonText );
86+
87+
if ( buttonIcon ) {
88+
buttonIcon.className = iconClass;
89+
buttonInner.appendChild( buttonIcon );
90+
}
7791

78-
el.wrapInner( wrap );
92+
while ( e.firstChild ) {
93+
buttonText.appendChild( e.firstChild );
94+
}
95+
96+
e.appendChild( buttonInner );
7997

8098
// TODO obviously it would be nice to pull this element out instead of
8199
// retrieving it from the DOM again, but this change is much less obtrusive
82100
// and 1.0 draws nigh
83-
el.data( 'textWrapper', el.find( "." + textClass ) );
84-
});
101+
el.data( 'textWrapper', $( buttonText ) );
102+
}
103+
104+
return this;
85105
};
86106

87107
$.fn.buttonMarkup.defaults = {

0 commit comments

Comments
 (0)