Skip to content

Commit 285afd7

Browse files
author
Gabriel Schulhof
committed
createDom: New module
1 parent ec88ea2 commit 285afd7

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

js/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'jquery.hashchange.js',
2020
'widgets/page.js',
2121
'jquery.mobile.core.js',
22+
'jquery.mobile.createDom.js',
2223
'widgets/loader.js',
2324
'events/navigate.js',
2425
'navigation/path.js',

js/jquery.mobile.createDom.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
2+
//>>description: Create an array of DOM elements from a JSON description
3+
//>>label: createDom
4+
//>>group: Core
5+
6+
define( [ "jquery.mobile.core" ], function( $ ) {
7+
//>>excludeEnd("jqmBuildExclude");
8+
(function( $, undefined ) {
9+
10+
//el can be one of two things:
11+
// 1. { tagname: [ { attr: value, ... }, [ children ]] }
12+
// 2. "a string"
13+
function mkEl( el ) {
14+
var ret, key, idx, attr, children, child;
15+
16+
if ( $.type( el ) === "string" ) {
17+
ret = document.createTextNode( el );
18+
} else {
19+
for ( key in el ) {
20+
ret = document.createElement( key );
21+
for( idx in el[ key ] ) {
22+
if ( $.type( el[ key ][ idx ] ) === "object" ) {
23+
for( attr in el[ key ][ idx ] ) {
24+
ret.setAttribute( attr, el[ key ][ idx ][ attr ] );
25+
}
26+
} else if ( $.type( el[ key ][ idx ] ) === "array" ) {
27+
children = mkChildren( el[ key ][ idx ] );
28+
for( child in children ) {
29+
ret.appendChild( children[ child ] );
30+
}
31+
}
32+
}
33+
}
34+
}
35+
36+
return ret;
37+
}
38+
39+
function mkChildren( c ) {
40+
var ret = [], idx;
41+
42+
for ( idx in c ) {
43+
ret.push( mkEl( c[ idx ] ) );
44+
}
45+
46+
return ret;
47+
}
48+
49+
$.mobile.createDom = function( j ) {
50+
return ( ( $.type( j ) === "object" ) ? mkEl( j ) :
51+
( ( $.type( j ) === "array" ) ? mkChildren( j ) :
52+
undefined ) );
53+
};
54+
55+
})( jQuery );
56+
//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
57+
});
58+
//>>excludeEnd("jqmBuildExclude");

0 commit comments

Comments
 (0)