Skip to content

Commit

Permalink
Merge remote branch 'abraao/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
cmilfont committed Jan 6, 2012
2 parents 7f21cfc + 7cc7833 commit 0208b01
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 10 deletions.
39 changes: 34 additions & 5 deletions lib/jsonform.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@
for(var property in el) {
if(el[property] || el[property] === 0) {
var value = el[property];
if( typeof value == "object") {
if( typeof value == "object" && !($.isArray(value) && value.every(function(val){return typeof val != "object"; })) ) {
var parent = (!father)? property : father + "\\." + property;
if($.isArray(value)) {
for(var item in value) {
if(value[item]) {
parent_arr = parent + "\\["+item+"\\]";
var parent_arr = parent + "\\["+item+"\\]";
roam(value[item], parent_arr);
}
}
Expand All @@ -123,7 +123,18 @@
var name = (father)? father + "\\." + property : property;
var query = "[name='" + name + "']";
if(byId) { query = ("#" + name); }
self.find(query).val(value);

var item = self.find(query);

if(item.length === 0){
var selector = query.replace(/\\\[(\d+)?\\\]/g, "\\[\\]"),
numChave = name.replace(/[^\\\[(\d+)?\\\]]/g, ""),
index = numChave.replace(/[^\d+]/g, "");

item = self.find(selector).eq(index);
}

item.val(value);
}
}
}
Expand Down Expand Up @@ -177,13 +188,31 @@
for(var property in source) {
if(typeof source[property] === 'object' &&
typeof merged[property] !== "undefined") {
merge(merged[property], source[property]);

if($.isArray(merged[property]) && $.isArray(source[property]) && !Object.Equals(merged[property], source[property].clean()))
merged[property].push(source[property][0]);
else
merge(merged[property], source[property]);
} else {
merged[property] = source[property];
}
}
return merged;
})
});


Object.Equals = Object.Equals || /*bool*/ function(obj1, obj2){
return JSON.stringify(obj1) === JSON.stringify(obj2);
};

Array.prototype.clean = function() {

for (var i = 0; i < this.length; i++) {
if (this[i] === null || this[i] === undefined) {
this.splice(i, 1);
i--;
}
}
return this;
};
})(jQuery);
4 changes: 2 additions & 2 deletions runspecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ global.jQuery = require("jquery").create(window);
var jasmine=require('jasmine-node');

//What we're testing
//require(__dirname + "/lib/jsonform.js")
require(__dirname + "/minified/jsonform.min.js")
require(__dirname + "/lib/jsonform.js")
//require(__dirname + "/minified/jsonform.min.js")

var isVerbose = true;
var showColors = true;
Expand Down
33 changes: 31 additions & 2 deletions specs/array_has_no_index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,44 @@ describe("Get JSON with jsonform when array has no index", function(){
beforeEach( function () {
jQuery("body").html("");
var template = __dirname + '/templates/array_has_no_index.html';
var multiple = require('fs').readFileSync(template).toString()
var multiple = require('fs').readFileSync(template).toString();
jQuery(multiple).appendTo("body");
});

it('should have nested objects', function () {
jQuery("[name='partidas[].conta.codigo']").val(["1.01.0001", "1.02.0002"]);
jQuery("[name='partidas[].conta.codigo']:first").val(["1.01.0001", "1.02.0002"]);
var json = jQuery("form[name='jsonform']").getJSON();
var codigo = json.partidas[0].conta.codigo;
expect(codigo).toEqual([ '1.01.0001', '1.02.0002' ]);
});

it('should have nested objects with many selects', function () {
jQuery("[name='partidas[].conta.codigo']").eq(0).val(["1.01.0001", "1.02.0002"]);
jQuery("[name='partidas[].conta.codigo']").eq(1).val(["1.03.0001", "1.03.0002"]);

var json = jQuery("form[name='jsonform']").getJSON();

var codigoDaContaDaPartida1 = json.partidas[0].conta.codigo;
var codigoDaContaDaPartida2 = json.partidas[1].conta.codigo;

expect(codigoDaContaDaPartida1).toEqual([ '1.01.0001', '1.02.0002' ]);
expect(codigoDaContaDaPartida2).toEqual([ '1.03.0001', '1.03.0002' ]);
});

it('should have nested objects with many selects (using populate)', function () {
var obj = {
partidas:[
{conta:{codigo:['1.01.0001', '1.02.0002']} },
{conta:{codigo:['1.03.0001', '1.03.0002']} }
]
};

var json = jQuery("form[name='jsonform']").populate(obj).getJSON();

var codigoDaContaDaPartida1 = json.partidas[0].conta.codigo;
var codigoDaContaDaPartida2 = json.partidas[1].conta.codigo;

expect(codigoDaContaDaPartida1).toEqual([ '1.01.0001', '1.02.0002' ]);
expect(codigoDaContaDaPartida2).toEqual([ '1.03.0001', '1.03.0002' ]);
});
});
19 changes: 18 additions & 1 deletion specs/getjson_select_with_multiple.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe("Get JSON with jsonform when select is multiple", function(){
beforeEach( function () {
jQuery("body").html("");
var template = __dirname + '/templates/multiple.html';
var multiple = require('fs').readFileSync(template).toString()
var multiple = require('fs').readFileSync(template).toString();
jQuery(multiple).appendTo("body");
});

Expand All @@ -14,5 +14,22 @@ describe("Get JSON with jsonform when select is multiple", function(){
var codigo = json.partidas[0].conta.codigo;
expect(codigo).toEqual([ '1.01.0001', '1.02.0002' ]);
});

it('should have nested objects using method populate', function () {
var obj = {
partidas:[
{conta:{codigo:['1.01.0001', '1.02.0002']} },
{conta:{codigo:['1.03.0001', '1.03.0002']} }
]
};

var json = jQuery("form[name='jsonform']").populate(obj).getJSON();

var codigo1 = json.partidas[0].conta.codigo;
var codigo2 = json.partidas[1].conta.codigo;

expect(codigo1).toEqual([ '1.01.0001', '1.02.0002' ]);
expect(codigo2).toEqual([ '1.03.0001', '1.03.0002' ]);
});

});
9 changes: 9 additions & 0 deletions specs/templates/multiple.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
<option value='1.02.0002'>Compras</option>
</select>
</input>
</p>
<p>
<label for='partidas[1].conta.codigo'>Débito:</label>
<select name='partidas[1].conta.codigo' multiple="multiple">
<option value='1.03.0001'>Caixa</option>
<option value='1.03.0002'>Banco</option>
<option value='1.03.0003'>Compras</option>
</select>
</input>
</p>
<p>
<input type='submit' name='submit'/>
Expand Down

0 comments on commit 0208b01

Please sign in to comment.