Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base: dea975e36f
...
compare: 1951e15dfe
  • 2 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Showing with 33 additions and 16 deletions.
  1. +15 −0 README.md
  2. +17 −15 src/jquery.jqml.js
  3. +1 −1  src/jquery.jqml.min.js
View
15 README.md
@@ -39,6 +39,21 @@ $.jqml([ 'table', (function( data ) {
}( data ))]);
```
+While passing an array of elements isn't technically correct JsonML, it makes for much easier templating.
+
+```javascript
+$.jqml([ 'div', (function( strings ) {
+ // notice there is no element in the first array item
+ var ptags = [];
+ for ( var i = 0; i < strings.length; i++ ) {
+ ptags.push([ 'p', strings[i]]);
+ }
+ // see how ptags is incorrect JsonML, but so much easier:
+ // ptags == [[ 'p', 'hi' ],[ 'p', 'yall!' ]]
+ return ptags;
+}([ 'hi', 'yall!' ]))]);
+```
+
If you have a problem, post an issue.
The plugin is super light weight, under 1K minified, so troubleshooting shouldn't be too hard.
And let me know if you have any features/improvements you'd like to see.
View
32 src/jquery.jqml.js
@@ -4,49 +4,51 @@
* MIT License: http://www.opensource.org/licenses/mit-license.php */
(function( $, document ) {
+
function createObj( elem ) {
+ // generate new fragment to store all generated
var fragment = document.createDocumentFragment(),
- i = 0, selector;
-
+ i = 0, j, selector;
// check if is an element or array of elements
if ( typeof elem[0] == 'string' ) {
selector = document.createElement( elem[0] );
i = 1;
};
-
+ // loop through all elements in array
for ( ; i < elem.length; i++ ) {
-
// if array create new element
- if ( $.isArray( elem[i] ) ) {
- fragment.appendChild( createObj( elem[i] ) );
-
+ if ( $.isArray( elem[i] )) {
+ // to simplify creation of templates, check for array of elements
+ if ( $.isArray( elem[i][0] )) {
+ for ( j = 0; j < elem[i].length; j++ ) {
+ fragment.appendChild( createObj( elem[i][j] ));
+ }
+ } else {
+ fragment.appendChild( createObj( elem[i] ));
+ }
// if object set element attributes
- } else if ( $.isPlainObject( elem[i] ) ) {
+ } else if ( $.isPlainObject( elem[i] )) {
+ // trick to have jQuery assign attributes without creating a new jQuery object
$.fn.attr.call( [selector], elem[i], true );
-
// if string or number insert text node
} else if ( typeof elem[i] == 'number' || typeof elem[i] == 'string' ) {
- fragment.appendChild( document.createTextNode( elem[i] ) );
-
+ fragment.appendChild( document.createTextNode( elem[i] ));
// if is an element append to fragment
} else if ( elem[i].nodeType ) {
fragment.appendChild( elem[i] );
};
};
-
// if a selector is set append children and return
if ( selector ) {
selector.appendChild( fragment );
return selector;
};
-
// otherwise return children of fragment
return fragment.childNodes;
};
$.jqml = function( arg ) {
-
// return new jQuery object of elements
- return $( createObj( arg ) );
+ return $( createObj( arg ));
};
})( jQuery, document );
View
2  src/jquery.jqml.min.js
@@ -2,4 +2,4 @@
* Author: Trevor Norris
* This document is licensed as free software under the terms of the
* MIT License: http://www.opensource.org/licenses/mit-license.php */
-(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);
+(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.