Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

docs added

  • Loading branch information...
commit bde21557040bdb76f834990a92e835980562844a 1 parent 4b189e5
@cmilfont cmilfont authored
View
39 README
@@ -1,6 +1,33 @@
-Install nodejs and npm https://github.com/joyent/node/wiki/Installation
-git clone git://github.com/milfont/jsonform.git
-cd jsonform
-npm install
-sudo chmod 777 testrunner
-./testrunner
+Example: with ID for legacy code
+ var lancamento = {
+ empresa: {id: 2, name: "Teste"},
+ partidas: [
+ {conta: {codigo:"1.02.0002", nome: "Compras"}, natureza: "1"},
+ {conta: {codigo:"1.02.0001", nome: "Banco"}, natureza: "-1"}
+ ],
+ description: "Teste",
+ value: "125,67",
+ date: "12/03/1999"
+ };
+ $('#form_id').populate(lancamento, true);
+
+Example: with name [default]
+ var lancamento = {
+ empresa: {id: 2, name: "Teste"},
+ partidas: [
+ {conta: {codigo:"1.02.0002", nome: "Compras"}, natureza: "1"},
+ {conta: {codigo:"1.02.0001", nome: "Banco"}, natureza: "-1"}
+ ],
+ description: "Teste",
+ value: "125,67",
+ date: "12/03/1999"
+ };
+ $("[name='form_name'").populate(lancamento);
+
+Running Tests:
+ Install nodejs and npm https://github.com/joyent/node/wiki/Installation
+ git clone git://github.com/milfont/jsonform.git
+ cd jsonform
+ npm install
+ sudo chmod 777 testrunner
+ ./testrunner
View
360 docs/index.html
@@ -0,0 +1,360 @@
+<html>
+ <head>
+ <title>JSONForm</title>
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
+ <style>body {
+ margin: 0;
+ padding: 0;
+ font: 14px/1.5 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif;
+ color: #252519;
+}
+a {
+ color: #252519;
+}
+a:hover {
+ text-decoration: underline;
+ color: #19469D;
+}
+p {
+ margin: 12px 0;
+}
+h1, h2, h3 {
+ margin: 0;
+ padding: 0;
+}
+table#source {
+ width: 100%;
+ border-collapse: collapse;
+}
+table#source td:first-child {
+ padding: 30px 40px 30px 40px;
+ vertical-align: top;
+}
+table#source td:first-child,
+table#source td:first-child pre {
+ width: 450px;
+}
+table#source td:last-child {
+ padding: 30px 0 30px 40px;
+ border-left: 1px solid #E5E5EE;
+ background: #F5F5FF;
+}
+table#source tr {
+ border-bottom: 1px solid #E5E5EE;
+}
+table#source tr.filename {
+ padding-top: 40px;
+ border-top: 1px solid #E5E5EE;
+}
+table#source tr.filename td:first-child {
+ text-transform: capitalize;
+}
+table#source tr.filename td:last-child {
+ font-size: 12px;
+}
+table#source tr.filename h2 {
+ margin: 0;
+ padding: 0;
+ cursor: pointer;
+}
+table#source tr.code h1,
+table#source tr.code h2,
+table#source tr.code h3 {
+ margin-top: 30px;
+ font-family: "Lucida Grande", "Helvetica Nueue", Arial, sans-serif;
+ font-size: 18px;
+}
+table#source tr.code h2 {
+ font-size: 16px;
+}
+table#source tr.code h3 {
+ font-size: 14px;
+}
+table#source tr.code ul {
+ margin: 15px 0 15px 35px;
+ padding: 0;
+}
+table#source tr.code ul li {
+ margin: 0;
+ padding: 1px 0;
+}
+table#source tr.code ul li p {
+ margin: 0;
+ padding: 0;
+}
+table#source tr.code td:first-child pre {
+ padding: 20px;
+}
+#ribbon {
+ position: fixed;
+ top: 0;
+ right: 0;
+}
+code .string { color: #219161; }
+code .regexp { color: #219161; }
+code .keyword { color: #954121; }
+code .number { color: #19469D; }
+code .comment { color: #bbb; }
+code .this { color: #19469D; }</style>
+ <script>
+ $(function(){
+ $('tr.code').hide();
+ $('tr.filename').toggle(function(){
+ $(this).nextUntil('.filename').fadeIn();
+ }, function(){
+ $(this).nextUntil('.filename').fadeOut();
+ });
+ });
+ </script>
+ </head>
+ <body>
+<table id="source"><tbody><tr><td><h1>JSONForm</h1></td><td></td></tr><tr class="filename"><td><h2 id="lib/jsonform.js"><a href="#">jsonform</a></h2></td><td>lib/jsonform.js</td></tr><tr class="code">
+<td class="docs">
+<ul><li><strong>name</strong>: <em>jsonform</em>
+.js: for jQuery JsonForm Plugin</li><li><strong>description</strong>: <em>Create</em>
+standard JSON from Forms or populate form with json using jQuery.</li><li><strong>requires</strong>: <em>jquery</em>
+.js</li><li><strong>link</strong>: <em>Github</em>
+: <a href="https://github.com/milfont/jsonform" title="https://github.com/milfont/jsonform">https://github.com/milfont/jsonform</a> </li><li><strong>author</strong>: <em>Christiano</em>
+Milfont <a href="mailto:cmilfont@gmail.com">cmilfont@gmail.com</a></li><li><strong>license</strong>: <em>Copyright</em>
+Milfont Consulting.
+Dual licensed under the MIT or GPL Version 2 licenses.
+http://jquery.org/license</li></ul>
+</td>
+<td class="code">
+<pre><code>(<span class="keyword">function</span> ($) {</code></pre>
+</td>
+</tr>
+<tr class="code">
+<td class="docs">
+<ul><li><strong>memberOf</strong>: <em>jQuery</em></li></ul>
+</td>
+<td class="code">
+<pre><code>$.<span class="variable">fn</span>.<span class="variable">extend</span>({</code></pre>
+</td>
+</tr>
+<tr class="code">
+<td class="docs">
+<ul><li><p><strong>version</strong>: <em>stable</em></p><pre><code> </code></pre></li></ul>
+</td>
+<td class="code">
+<pre><code><span class="variable">version</span>: <span class="string">'1.1.0'</span>,</code></pre>
+</td>
+</tr>
+<tr class="code">
+<td class="docs">
+<h2>Example</h2>
+
+<pre><code>$('#form_id').getJSON(true);
+
+ </code></pre>
+</td>
+<td class="code">
+
+</td>
+</tr>
+<tr class="code">
+<td class="docs">
+<h2>Example</h2>
+
+<pre><code>$("[name='formname']").getJSON();
+
+ </code></pre>
+</td>
+<td class="code">
+
+</td>
+</tr>
+<tr class="code">
+<td class="docs">
+<ul><li><p><strong>description</strong>: <em>Create</em>
+standard JSON from Forms with jQuery. Resolve nested objects.</p></li><li><p><strong>name</strong>: <em>getJSON</em></p></li></ul>
+
+<h2></h2>
+
+<ul><li><p><strong>param</strong>: <em>Boolean</em> byId find inputs with query by id</p></li><li><p><strong>returns</strong>: <em>Object</em> Object Literal [json]</p></li><li><p><strong>type</strong>: <em>jQuery</em></p></li><li><p><strong>cat</strong>: <em>Plugins</em>
+/jsonform
+ </p></li></ul>
+</td>
+<td class="code">
+<pre><code><span class="variable">getJSON</span>: <span class="keyword">function</span>(<span class="variable">byId</span>) {
+ <span class="keyword">var</span> <span class="variable">json</span> = {}, <span class="variable">self</span> = <span class="this">this</span>;
+ <span class="keyword">var</span> <span class="variable">config</span>;
+ <span class="keyword">if</span>(<span class="keyword">typeof</span> <span class="variable">byId</span> === <span class="string">'object'</span>) {
+ <span class="variable">config</span> = <span class="variable">byId</span>;
+ <span class="variable">byId</span> = (<span class="variable">config</span>.<span class="variable">byId</span>)? <span class="variable">config</span>.<span class="variable">byId</span> : <span class="variable">false</span>;
+ }
+ <span class="variable">self</span>.<span class="variable">find</span>(&<span class="variable">quot</span>;<span class="variable">input</span>,<span class="variable">textarea</span>,<span class="variable">select</span>&<span class="variable">quot</span>;).<span class="variable">each</span>( <span class="keyword">function</span>(<span class="variable">index</span>, <span class="variable">item</span>) {
+ <span class="keyword">var</span> <span class="variable">name</span> = (<span class="variable">byId</span>) ? $.<span class="variable">trim</span>(<span class="variable">item</span>.<span class="variable">id</span>) : $.<span class="variable">trim</span>(<span class="variable">item</span>.<span class="variable">name</span>);
+ <span class="keyword">if</span>(<span class="variable">name</span> !== &<span class="variable">quot</span>;&<span class="variable">quot</span>;) {
+ <span class="variable">self</span>.<span class="variable">merge</span>(<span class="variable">json</span>, <span class="variable">self</span>.<span class="variable">buildJson</span>(<span class="variable">name</span>, <span class="variable">item</span>.<span class="variable">value</span>, <span class="variable">config</span>));
+ }
+ });
+ <span class="keyword">return</span> <span class="variable">json</span>;
+ },</code></pre>
+</td>
+</tr>
+<tr class="code">
+<td class="docs">
+<h2>Example with ID for legacy code</h2>
+
+<pre><code>var lancamento = {
+ empresa: {id: 2, name: "Teste"},
+ partidas: [
+ {conta: {codigo:"1.02.0002", nome: "Compras"}, natureza: "1"},
+ {conta: {codigo:"1.02.0001", nome: "Banco"}, natureza: "-1"}
+ ],
+ description: "Teste",
+ value: "125,67",
+ date: "12/03/1999"
+};
+$('#form_id').populate(lancamento, true);
+ </code></pre>
+</td>
+<td class="code">
+
+</td>
+</tr>
+<tr class="code">
+<td class="docs">
+<h2>Example with name [default]</h2>
+
+<pre><code>var lancamento = {
+ empresa: {id: 2, name: "Teste"},
+ partidas: [
+ {conta: {codigo:"1.02.0002", nome: "Compras"}, natureza: "1"},
+ {conta: {codigo:"1.02.0001", nome: "Banco"}, natureza: "-1"}
+ ],
+ description: "Teste",
+ value: "125,67",
+ date: "12/03/1999"
+};
+$("[name='form_name'").populate(lancamento);
+ </code></pre>
+</td>
+<td class="code">
+
+</td>
+</tr>
+<tr class="code">
+<td class="docs">
+<ul><li><p><strong>name</strong>: <em>populate</em></p></li><li><p><strong>desc</strong>: <em>Populate</em>
+form with json using jQuery. Resolve nested objects.
+## </p></li><li><p><strong>param</strong>: <em>Object</em> json Standard JSON to populate FORM</p></li><li><p><strong>param</strong>: <em>Boolean</em> [optional] byId find inputs with query by id</p></li><li><p><strong>returns</strong>: <em>jQuery</em> jQuery</p></li><li><p><strong>type</strong>: <em>jQuery</em></p></li><li><p><strong>cat</strong>: <em>Plugins</em>
+/jsonform
+ </p></li></ul>
+</td>
+<td class="code">
+<pre><code><span class="variable">populate</span>: <span class="keyword">function</span>(<span class="variable">json</span>, <span class="variable">byId</span>) {
+ <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>;
+ (<span class="keyword">function</span> <span class="variable">roam</span>(<span class="variable">el</span>, <span class="variable">father</span>) {
+ <span class="keyword">for</span>(<span class="keyword">var</span> <span class="variable">property</span> <span class="keyword">in</span> <span class="variable">el</span>) {
+ <span class="keyword">if</span>(<span class="variable">el</span>[<span class="variable">property</span>] || <span class="variable">el</span>[<span class="variable">property</span>] === <span class="number integer">0</span>) {
+ <span class="keyword">var</span> <span class="variable">value</span> = <span class="variable">el</span>[<span class="variable">property</span>];
+ <span class="keyword">if</span>( <span class="keyword">typeof</span> <span class="variable">value</span> == &<span class="variable">quot</span>;<span class="variable">object</span>&<span class="variable">quot</span>;) {
+ <span class="keyword">var</span> <span class="variable">parent</span> = (!<span class="variable">father</span>)? <span class="variable">property</span> : <span class="variable">father</span> + &<span class="variable">quot</span>;\\.&<span class="variable">quot</span>; + <span class="variable">property</span>;
+ <span class="keyword">if</span>($.<span class="variable">isArray</span>(<span class="variable">value</span>)) {
+ <span class="keyword">for</span>(<span class="keyword">var</span> <span class="variable">item</span> <span class="keyword">in</span> <span class="variable">value</span>) {
+ <span class="keyword">if</span>(<span class="variable">value</span>[<span class="variable">item</span>]) {
+ <span class="variable">parent_arr</span> = <span class="variable">parent</span> + &<span class="variable">quot</span>;\\[&<span class="variable">quot</span>;+<span class="variable">item</span>+&<span class="variable">quot</span>;\\]&<span class="variable">quot</span>;;
+ <span class="variable">roam</span>(<span class="variable">value</span>[<span class="variable">item</span>], <span class="variable">parent_arr</span>);
+ }
+ }
+ } <span class="keyword">else</span> {
+ <span class="variable">roam</span>(<span class="variable">value</span>, <span class="variable">parent</span>);
+ }
+ <span class="variable">parent</span> = <span class="keyword">null</span>;
+ } <span class="keyword">else</span> {
+ <span class="keyword">var</span> <span class="variable">name</span> = (<span class="variable">father</span>)? <span class="variable">father</span> + &<span class="variable">quot</span>;\\.&<span class="variable">quot</span>; + <span class="variable">property</span> : <span class="variable">property</span>;
+ <span class="keyword">var</span> <span class="variable">query</span> = &<span class="variable">quot</span>;[<span class="variable">name</span>=<span class="string">'&quot; + name + &quot;'</span>]&<span class="variable">quot</span>;;
+ <span class="keyword">if</span>(<span class="variable">byId</span>) {
+ <span class="variable">query</span> = (&<span class="variable">quot</span>;#&<span class="variable">quot</span>; + <span class="variable">name</span>);
+ }
+ <span class="variable">self</span>.<span class="variable">find</span>(<span class="variable">query</span>).<span class="variable">val</span>(<span class="variable">value</span>);
+ }
+ }
+ }
+ })(<span class="variable">json</span>);
+ <span class="keyword">return</span> <span class="this">this</span>;
+ },</code></pre>
+</td>
+</tr>
+<tr class="code">
+<td class="docs">
+<ul><li><strong>name</strong>: <em>buildJson</em></li></ul>
+
+<h2></h2>
+
+<ul><li><p><strong>param</strong>: <em>String</em> id</p></li><li><p><strong>param</strong>: <em>String</em> valor</p></li><li><p><strong>param</strong>: <em>Object</em> config</p></li><li><p><strong>returns</strong>: <em>Object</em> Object Literal</p><pre><code> </code></pre></li></ul>
+</td>
+<td class="code">
+<pre><code><span class="variable">buildJson</span>: <span class="keyword">function</span>(<span class="variable">id</span>, <span class="variable">valor</span>, <span class="variable">config</span>) {
+ <span class="keyword">var</span> <span class="variable">hierarchy</span> = <span class="variable">id</span>.<span class="variable">split</span>(&<span class="variable">quot</span>;.&<span class="variable">quot</span>;);
+ <span class="keyword">var</span> <span class="variable">json</span> = {};
+ <span class="keyword">var</span> <span class="variable">prefix</span> = (<span class="variable">config</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">config</span>.<span class="variable">prefix</span>)? <span class="variable">config</span>.<span class="variable">prefix</span>:&<span class="variable">quot</span>;&<span class="variable">quot</span>;;
+ <span class="keyword">var</span> <span class="variable">suffix</span> = (<span class="variable">config</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">config</span>.<span class="variable">suffix</span>)? <span class="variable">config</span>.<span class="variable">suffix</span>:&<span class="variable">quot</span>;&<span class="variable">quot</span>;;
+ <span class="keyword">for</span>(<span class="keyword">var</span> <span class="variable">index</span> = <span class="variable">hierarchy</span>.<span class="variable">length</span>; <span class="variable">index</span> &<span class="variable">gt</span>; <span class="number integer">0</span>; <span class="variable">index</span>--) {
+ <span class="keyword">var</span> <span class="variable">temp</span> = {};
+ <span class="keyword">var</span> <span class="variable">name</span> = <span class="variable">hierarchy</span>[<span class="variable">index</span>-<span class="number integer">1</span>];
+ <span class="keyword">if</span>(<span class="variable">name</span> == <span class="variable">hierarchy</span>[<span class="variable">hierarchy</span>.<span class="variable">length</span>-<span class="number integer">1</span>]) {
+ <span class="variable">temp</span>[<span class="variable">name</span>] = <span class="variable">valor</span>;
+ } <span class="keyword">else</span> {
+ <span class="keyword">var</span> <span class="variable">arrayIndex</span> = <span class="variable">name</span>.<span class="variable">search</span>(<span class="regexp">/\[(\d+)\]/</span>);
+ <span class="keyword">if</span>(<span class="variable">arrayIndex</span> &<span class="variable">gt</span>; -<span class="number integer">1</span>) {
+ <span class="keyword">var</span> <span class="variable">arrayName</span> = <span class="variable">name</span>.<span class="variable">substring</span>(<span class="number integer">0</span>, <span class="variable">arrayIndex</span>);
+ <span class="variable">arrayName</span> = <span class="variable">prefix</span> + <span class="variable">arrayName</span> + <span class="variable">suffix</span>;
+ <span class="keyword">var</span> <span class="variable">position</span> = <span class="variable">name</span>.<span class="variable">substring</span>(<span class="variable">arrayIndex</span>+<span class="number integer">1</span>, <span class="variable">name</span>.<span class="variable">indexOf</span>(&<span class="variable">quot</span>;]&<span class="variable">quot</span>;));
+ <span class="keyword">var</span> <span class="variable">arr</span> = <span class="variable">temp</span>[<span class="variable">arrayName</span>] = [];
+ <span class="keyword">for</span>(<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">position</span>; <span class="variable">i</span>++) {
+ <span class="variable">arr</span>[<span class="variable">i</span>] = {};
+ }
+ <span class="variable">arr</span>[<span class="variable">position</span>] = <span class="variable">json</span>;
+ } <span class="keyword">else</span> {
+ <span class="variable">temp</span>[<span class="variable">prefix</span> + <span class="variable">name</span> + <span class="variable">suffix</span>] = <span class="variable">json</span>;
+ }
+ }
+ <span class="variable">json</span> = <span class="variable">temp</span>;
+ }
+ <span class="keyword">return</span> <span class="variable">json</span>;
+ },</code></pre>
+</td>
+</tr>
+<tr class="code">
+<td class="docs">
+<ul><li><strong>name</strong>: <em>merge</em></li></ul>
+
+<h2></h2>
+
+<ul><li><p><strong>param</strong>: <em>Object</em> merged</p></li><li><p><strong>param</strong>: <em>Object</em> source</p></li><li><p><strong>returns</strong>: <em>Object</em> Object Literal</p><pre><code> </code></pre></li></ul>
+</td>
+<td class="code">
+<pre><code><span class="variable">merge</span>: (<span class="keyword">function</span> <span class="variable">merge</span>(<span class="variable">merged</span>, <span class="variable">source</span>) {
+ <span class="keyword">for</span>(<span class="keyword">var</span> <span class="variable">property</span> <span class="keyword">in</span> <span class="variable">source</span>) {
+ <span class="keyword">if</span>(<span class="keyword">typeof</span> <span class="variable">source</span>[<span class="variable">property</span>] === <span class="string">'object'</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>;
+ <span class="keyword">typeof</span> <span class="variable">merged</span>[<span class="variable">property</span>] !== &<span class="variable">quot</span>;<span class="variable">undefined</span>&<span class="variable">quot</span>;) {
+ <span class="variable">merge</span>(<span class="variable">merged</span>[<span class="variable">property</span>], <span class="variable">source</span>[<span class="variable">property</span>]);
+ } <span class="keyword">else</span> {
+ <span class="variable">merged</span>[<span class="variable">property</span>] = <span class="variable">source</span>[<span class="variable">property</span>];
+ }
+ }
+ <span class="keyword">return</span> <span class="variable">merged</span>;
+ }),</code></pre>
+</td>
+</tr>
+<tr class="code">
+<td class="docs">
+<ul><li><strong>name</strong>: <em>clone</em></li></ul>
+
+<h2></h2>
+
+<ul><li><p><strong>param</strong>: <em>Object</em> source</p></li><li><p><strong>returns</strong>: <em>Object</em> Object Literal</p><pre><code> </code></pre></li></ul>
+</td>
+<td class="code">
+<pre><code><span class="variable">clone</span>: <span class="keyword">function</span>(<span class="variable">source</span>) {
+ <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">merge</span>({}, <span class="variable">source</span>);
+ }
+ });
+
+})(<span class="variable">jQuery</span>);</code></pre>
+</td>
+</tr> </body>
+</html></tbody></table>
View
128 lib/jsonform.js
@@ -1,31 +1,41 @@
-/*!
- * jsonform.js: for jQuery JsonForm Plugin 1.1.0
- * Create standard JSON from Forms or populate form with json using jQuery.
- * Requires jquery.js
- * https://github.com/milfont/jsonform
- *
- * Copyright Milfont Consulting.
+/**
+ * @name jsonform.js: for jQuery JsonForm Plugin
+ * @description Create standard JSON from Forms or populate form with json using jQuery.
+ * @requires jquery.js
+ * @link Github: [https://github.com/milfont/jsonform](https://github.com/milfont/jsonform "https://github.com/milfont/jsonform")
+ * @author Christiano Milfont <cmilfont@gmail.com>
+ * @license Copyright Milfont Consulting.
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*/
(function ($) {
+ /** @memberOf jQuery */
$.fn.extend({
- version:"1.1.0",
+ /**
+ * @version stable
+ */
+ version: '1.1.0',
+
/**
*
- * @example
- * $('#form_id').getJSON(true);
+ * Example:
+ * $('#form_id').getJSON(true);
*
- * @example with name [default]
- * $("[name='formname']").getJSON();
+ */
+
+ /**
+ * Example:
+ * $("[name='formname']").getJSON();
*
- * @desc Create standard JSON from Forms with jQuery.
- * Resolve nested objects.
+ */
+
+ /**
+ * @description Create standard JSON from Forms with jQuery. Resolve nested objects.
*
* @name getJSON
- * @param Boolean byId find inputs with query by id
- * @return json
+ * @param {Boolean} byId find inputs with query by id
+ * @returns {Object} Object Literal [json]
* @type jQuery
* @cat Plugins/jsonform
*/
@@ -44,42 +54,44 @@
});
return json;
},
+
/**
*
- * @example
- * var lancamento = {
- * empresa: {id: 2, name: "Teste"},
- * partidas: [
- * {conta: {codigo:"1.02.0002", nome: "Compras"}, natureza: "1"},
- * {conta: {codigo:"1.02.0001", nome: "Banco"}, natureza: "-1"}
- * ],
- * description: "Teste",
- * value: "125,67",
- * date: "12/03/1999"
- * };
- * $('#form_id').populate(lancamento, true);
- *
- * @example with name [default]
- * var lancamento = {
- * empresa: {id: 2, name: "Teste"},
- * partidas: [
- * {conta: {codigo:"1.02.0002", nome: "Compras"}, natureza: "1"},
- * {conta: {codigo:"1.02.0001", nome: "Banco"}, natureza: "-1"}
- * ],
- * description: "Teste",
- * value: "125,67",
- * date: "12/03/1999"
- * };
- * $("[name='form_name'").populate(lancamento);
- *
- *
- * @desc Populate form with json using jQuery.
- * Resolve nested objects.
- *
+ * Example: with ID for legacy code
+ * var lancamento = {
+ * empresa: {id: 2, name: "Teste"},
+ * partidas: [
+ * {conta: {codigo:"1.02.0002", nome: "Compras"}, natureza: "1"},
+ * {conta: {codigo:"1.02.0001", nome: "Banco"}, natureza: "-1"}
+ * ],
+ * description: "Teste",
+ * value: "125,67",
+ * date: "12/03/1999"
+ * };
+ * $('#form_id').populate(lancamento, true);
+ */
+
+ /**
+ * Example: with name [default]
+ * var lancamento = {
+ * empresa: {id: 2, name: "Teste"},
+ * partidas: [
+ * {conta: {codigo:"1.02.0002", nome: "Compras"}, natureza: "1"},
+ * {conta: {codigo:"1.02.0001", nome: "Banco"}, natureza: "-1"}
+ * ],
+ * description: "Teste",
+ * value: "125,67",
+ * date: "12/03/1999"
+ * };
+ * $("[name='form_name'").populate(lancamento);
+ */
+
+ /**
* @name populate
- * @param Object json Standard JSON to populate FORM
- * @param Boolean byId find inputs with query by id
- * @return jQuery
+ * @desc Populate form with json using jQuery. Resolve nested objects.
+ * @param {Object} json Standard JSON to populate FORM
+ * @param {Boolean} [optional] byId find inputs with query by id
+ * @returns {jQuery} jQuery
* @type jQuery
* @cat Plugins/jsonform
*/
@@ -115,6 +127,13 @@
})(json);
return this;
},
+ /**
+ * @name buildJson
+ * @param {String} id
+ * @param {String} valor
+ * @param {Object} config
+ * @returns {Object} Object Literal
+ */
buildJson: function(id, valor, config) {
var hierarchy = id.split(".");
var json = {};
@@ -144,6 +163,12 @@
}
return json;
},
+ /**
+ * @name merge
+ * @param {Object} merged
+ * @param {Object} source
+ * @returns {Object} Object Literal
+ */
merge: (function merge(merged, source) {
for(var property in source) {
if(typeof source[property] === 'object' &&
@@ -155,6 +180,11 @@
}
return merged;
}),
+ /**
+ * @name clone
+ * @param {Object} source
+ * @returns {Object} Object Literal
+ */
clone: function(source) {
return this.merge({}, source);
}
View
5 minified/jsonform.min.js
@@ -1,4 +1,9 @@
/* jsonform.js */
+/*
+ Copyright Milfont Consulting.
+ Dual licensed under the MIT or GPL Version 2 licenses.
+ http://jquery.org/license
+*/
(function(k){k.fn.extend({version:"1.1.0",getJSON:function(b){var i={},a=this,e;typeof b==="object"&&(e=b,b=e.byId?e.byId:!1);a.find("input,textarea,select").each(function(f,j){var g=b?k.trim(j.id):k.trim(j.name);g!==""&&a.merge(i,a.buildJson(g,j.value,e))});return i},populate:function(b,i){var a=this;(function f(b,g){for(var c in b)if(b[c]||b[c]===0){var d=b[c];if(typeof d=="object"){var h=!g?c:g+"\\."+c;if(k.isArray(d))for(var l in d)d[l]&&(parent_arr=h+"\\["+l+"\\]",f(d[l],parent_arr));else f(d,
h)}else{var h=g?g+"\\."+c:c,m="[name='"+h+"']";i&&(m="#"+h);a.find(m).val(d)}}})(b);return this},buildJson:function(b,i,a){for(var b=b.split("."),e={},f=a&&a.prefix?a.prefix:"",a=a&&a.suffix?a.suffix:"",j=b.length;j>0;j--){var g={},c=b[j-1];if(c==b[b.length-1])g[c]=i;else{var d=c.search(/\[(\d+)\]/);if(d>-1){for(var h=c.substring(0,d),h=f+h+a,c=c.substring(d+1,c.indexOf("]")),h=g[h]=[],d=0;d<c;d++)h[d]={};h[c]=e}else g[f+c+a]=e}e=g}return e},merge:function i(a,e){for(var f in e)typeof e[f]==="object"&&
typeof a[f]!=="undefined"?i(a[f],e[f]):a[f]=e[f];return a},clone:function(i){return this.merge({},i)}})})(jQuery);
View
3  package.json
@@ -4,7 +4,8 @@
, "main" : "runspecs.js"
, "dependencies" :
{
- "ready.js" : "1.3.1",
+ "dox" : "0.0.5",
+ "ready.js" : "1.3.3",
"jsdom" : "0.2.0",
"jquery" : "1.5.1",
"jasmine-node" : "https://github.com/milfont/jasmine-node/tarball/master"
View
5 testrunner
@@ -5,7 +5,10 @@
#node-jscoverage lib lib-cov
#TODO
-./build
+dox --title JSONForm lib/jsonform.js > docs/index.html
+
+#https://github.com/dsimard/ready.js
+readyjs ready.conf.js
#execute tests
node runspecs.js
Please sign in to comment.
Something went wrong with that request. Please try again.