Giter VIP home page Giter VIP logo

jqml's Introduction

jqml - jQuery/JavaScript JsonML Translator

In src/ you will find the following files:

  • jqml.js - Create DOM from JsonML, requires no additional libraries.
  • jqml.min.js - Minified jqml.js.
  • jquery.jqml.js - jqml implementation specific to jQuery.
  • jquery.jqml.min.js - Minified jquery.jqml.js.

Below are examples of how to use the two libraries. The only syntactical difference is that one uses jQuery.jqml() and the other uses jqml().

To create a new element just pass in the JsonML.

$.jqml([ 'div', {
	'id' : 'mydiv',
	'class' : 'colors borders'
}, [ 'p' ]]);

Say you need to create a template that prints table rows based on data received from the server. Well, just create an immediately executing anonymous function in the JsonML for a quick little template.

$.jqml([ 'table', (function( data ) {
	var dataRows = [ 'tbody' ];
	for ( var i = 0; i < data.length; i++ ) {
		dataRows.push([ 'tr', [ 'td', data[ i ]]]);
	}
	return dataRows;
}( data ))]);

While passing an array of elements isn't technically correct JsonML, it makes for much easier templating.

$.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!' ]))]);

The jQuery plugin also ties into the the jQuery event model. So you can attach events to the elements as they're being created.

$.jqml([ 'nav', [ 'a', {
	'href' : '#link',
	'click' : function( e ) {
		e.preventDefault();
		// do more stuff
	}
}]]).prependTo( 'body' );

If you have a problem, post an issue. The plugins are 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.

jqml's People

Contributors

trevnorris avatar jamesb192 avatar cdrubin avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.