Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 3 files changed
  • 0 comments
  • 1 contributor

Showing 3 changed files with 33 additions and 16 deletions. Show diff stats Hide diff stats

  1. +15 0 README.md
  2. +17 15 src/jquery.jqml.js
  3. +1 1  src/jquery.jqml.min.js
15 README.md
Source Rendered
@@ -39,6 +39,21 @@ $.jqml([ 'table', (function( data ) {
39 39 }( data ))]);
40 40 ```
41 41
  42 +While passing an array of elements isn't technically correct JsonML, it makes for much easier templating.
  43 +
  44 +```javascript
  45 +$.jqml([ 'div', (function( strings ) {
  46 + // notice there is no element in the first array item
  47 + var ptags = [];
  48 + for ( var i = 0; i < strings.length; i++ ) {
  49 + ptags.push([ 'p', strings[i]]);
  50 + }
  51 + // see how ptags is incorrect JsonML, but so much easier:
  52 + // ptags == [[ 'p', 'hi' ],[ 'p', 'yall!' ]]
  53 + return ptags;
  54 +}([ 'hi', 'yall!' ]))]);
  55 +```
  56 +
42 57 If you have a problem, post an issue.
43 58 The plugin is super light weight, under 1K minified, so troubleshooting shouldn't be too hard.
44 59 And let me know if you have any features/improvements you'd like to see.
32 src/jquery.jqml.js
@@ -4,49 +4,51 @@
4 4 * MIT License: http://www.opensource.org/licenses/mit-license.php */
5 5
6 6 (function( $, document ) {
  7 +
7 8 function createObj( elem ) {
  9 + // generate new fragment to store all generated
8 10 var fragment = document.createDocumentFragment(),
9   - i = 0, selector;
10   -
  11 + i = 0, j, selector;
11 12 // check if is an element or array of elements
12 13 if ( typeof elem[0] == 'string' ) {
13 14 selector = document.createElement( elem[0] );
14 15 i = 1;
15 16 };
16   -
  17 + // loop through all elements in array
17 18 for ( ; i < elem.length; i++ ) {
18   -
19 19 // if array create new element
20   - if ( $.isArray( elem[i] ) ) {
21   - fragment.appendChild( createObj( elem[i] ) );
22   -
  20 + if ( $.isArray( elem[i] )) {
  21 + // to simplify creation of templates, check for array of elements
  22 + if ( $.isArray( elem[i][0] )) {
  23 + for ( j = 0; j < elem[i].length; j++ ) {
  24 + fragment.appendChild( createObj( elem[i][j] ));
  25 + }
  26 + } else {
  27 + fragment.appendChild( createObj( elem[i] ));
  28 + }
23 29 // if object set element attributes
24   - } else if ( $.isPlainObject( elem[i] ) ) {
  30 + } else if ( $.isPlainObject( elem[i] )) {
  31 + // trick to have jQuery assign attributes without creating a new jQuery object
25 32 $.fn.attr.call( [selector], elem[i], true );
26   -
27 33 // if string or number insert text node
28 34 } else if ( typeof elem[i] == 'number' || typeof elem[i] == 'string' ) {
29   - fragment.appendChild( document.createTextNode( elem[i] ) );
30   -
  35 + fragment.appendChild( document.createTextNode( elem[i] ));
31 36 // if is an element append to fragment
32 37 } else if ( elem[i].nodeType ) {
33 38 fragment.appendChild( elem[i] );
34 39 };
35 40 };
36   -
37 41 // if a selector is set append children and return
38 42 if ( selector ) {
39 43 selector.appendChild( fragment );
40 44 return selector;
41 45 };
42   -
43 46 // otherwise return children of fragment
44 47 return fragment.childNodes;
45 48 };
46 49
47 50 $.jqml = function( arg ) {
48   -
49 51 // return new jQuery object of elements
50   - return $( createObj( arg ) );
  52 + return $( createObj( arg ));
51 53 };
52 54 })( jQuery, document );
2  src/jquery.jqml.min.js
@@ -2,4 +2,4 @@
2 2 * Author: Trevor Norris
3 3 * This document is licensed as free software under the terms of the
4 4 * MIT License: http://www.opensource.org/licenses/mit-license.php */
5   -(function(c,a){function b(g){var e=a.createDocumentFragment(),f=0,d;if(typeof g[0]=="string"){d=a.createElement(g[0]);f=1}for(;f<g.length;f++){if(c.isArray(g[f])){e.appendChild(b(g[f]))}else{if(c.isPlainObject(g[f])){c.fn.attr.call([d],g[f],true)}else{if(typeof g[f]=="number"||typeof g[f]=="string"){e.appendChild(a.createTextNode(g[f]))}else{if(g[f].nodeType){e.appendChild(g[f])}}}}}if(d){d.appendChild(e);return d}return e.childNodes}c.jqml=function(d){return c(b(d))}})(jQuery,document);
  5 +(function(c,a){function b(h){var f=a.createDocumentFragment(),g=0,e,d;if(typeof h[0]=="string"){d=a.createElement(h[0]);g=1}for(;g<h.length;g++){if(c.isArray(h[g])){if(c.isArray(h[g][0])){for(e=0;e<h[g].length;e++){f.appendChild(b(h[g][e]))}}else{f.appendChild(b(h[g]))}}else{if(c.isPlainObject(h[g])){c.fn.attr.call([d],h[g],true)}else{if(typeof h[g]=="number"||typeof h[g]=="string"){f.appendChild(a.createTextNode(h[g]))}else{if(h[g].nodeType){f.appendChild(h[g])}}}}}if(d){d.appendChild(f);return d}return f.childNodes}c.jqml=function(d){return c(b(d))}})(jQuery,document);

No commit comments for this range

Something went wrong with that request. Please try again.