Skip to content

Commit

Permalink
fix #7: handling of simple array in the first level
Browse files Browse the repository at this point in the history
  • Loading branch information
corinis committed Jan 14, 2014
1 parent 16512f9 commit c10dbf3
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 41 deletions.
3 changes: 2 additions & 1 deletion src/jquery.jsForm.controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,8 @@
numberformat = i18n.number;
else if($(document).data().i18n && $(document).data().i18n.number)
numberformat = $(document).data().i18n.number;

// make sure num is a string
num = "" + num;
// get rid of the grouping seperator (if any exist)
if(num.indexOf(numberformat.groupingSeparator) !== -1)
num = num.replace(numberformat.groupingSeparator, "", "g");
Expand Down
72 changes: 36 additions & 36 deletions src/jquery.jsForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,24 @@
* @param msg the message to pring
* @private
*/
JsForm.prototype._debug = function(msg) {
if(typeof console !== "undefined") {
console.log("JsForm: " + msg);
JsForm.prototype._debug = function(msg, param) {
try {
if (!window.console || !window.console.log)
return

var p = null;
if($.isPlainObject(param)) {
p = JSON.stringify(param, null, " ");
} else
p = param;

if(!p) {
p = "";
}

window.console.log(msg + p);
} catch(ex) {
// ignore
}
};

Expand Down Expand Up @@ -325,28 +340,20 @@
// and has a template
if(tmpl) {
var line = tmpl.clone(true);
$(this).append(line);
$(line).addClass("POJO");
$(line).data("pojo", {});
$(line).data().pojo = {};
$(this).append(line);

that._addCollectionControls(line);

// trigger a callback
$(this).trigger("addCollection", [line, $(line).data().pojo]);

// the new entry has as index the count of all "lines"
var idx = $(this).children(".POJO").length;

// fill the line with data
that._fillData(line, $(line).data().pojo, fieldName.substring(fieldName.indexOf('.')+1), idx);

// its possible to have "sub" collections
that._initCollection(line, fieldName.substring(fieldName.indexOf('.')+1));

// trigger a callback after the data has been rendered)
$(this).trigger("postAddCollection", [line, $(line).data().pojo]);


}
});
});
Expand Down Expand Up @@ -1176,26 +1183,18 @@
}

fieldname = fieldname.substring((prefix+".").length);
if(fieldname.length < 1) {
return;
}

var colParent = that._getParent(pojo, fieldname, true);

// get only the last part
if(fieldname.indexOf('.') !== -1) {
fieldname = fieldname.substring(fieldname.lastIndexOf('.') + 1);
}

// clear the collection
colParent[fieldname] = [];

// go through all direct childs - each one is an element
$(this).children().each(function(){
if(!ignoreInvalid && invalid) {
return;
}

var ele = {};
ele = that._createPojoFromInput($(this), fieldname, ele);

Expand All @@ -1206,6 +1205,8 @@
if(!that._isEmpty(ele)) {
if($(".invalid", this).length > 0) {
invalid = true;
if(!ignoreInvalid)
return false;
}
colParent[fieldname].push(ele);
} else {
Expand Down Expand Up @@ -1785,7 +1786,7 @@
// a string
return (pojo === "" || pojo === " ");
};

/**
* compares two objects. note: empty string or null is the same as not existant
* @param a the object to compare
Expand All @@ -1809,15 +1810,13 @@

var p = null;
for(p in a) {
if(typeof(b[p]) === 'undefined' && a[p] !== null && a[p] !== "") {
if(typeof(b[p]) === 'undefined' && a[p] !== null && a[p] !== "" && a[p].length != 0) {
// 0 == undefined
if((a[p] === "0" || a[p] === 0) && !b[p])
continue;
return false;
}
}

for(p in a) {

if (a[p]) {
switch(typeof(a[p])) {
case 'object':
Expand All @@ -1841,16 +1840,17 @@
if((a === true || a === false) && a !== b) {
return false;
}

if(("" + a[p]).length !== ("" +b[p]).length) {
return false;
}
if(!isNaN(a[p]) || !isNaN(b[p])) {
if(Number(a[p]) !== Number(b[p])) {
return false;
if(Math.abs(Number(a[p]) - Number(b[p])) < 0.0000001) {
break;
}
return false;
}


if(("" + a[p]).length !== ("" +b[p]).length) {
return false;
}

if (a[p] !== b[p] && Number(a[p]) !== Number(b[p])) {
return false;
}
Expand All @@ -1872,7 +1872,7 @@
};

/**
* Compares a pojo with form fields
* Compares a pojo with the current generated object
* @param pojo the pojo to compare with
* @return true if any change between formfields and the pojo is found
*/
Expand Down Expand Up @@ -1953,7 +1953,7 @@
*/
JsForm.prototype.fill = function(pojo) {
// set the new data
this.options.data = pojo;
this.options.data = $.extend({}, pojo);
// fill everything
this._fill();
};
Expand Down
60 changes: 56 additions & 4 deletions test/jquery.jsForm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ test("collection in collection test", function(){
basicForm.remove();
});

test("simple arrays", function(){
test("simple arrays and checkboxes", function(){
// html code for basic form
var basicForm = $('<div>\n'+
'<input type="checkbox" class="array" name="data.checks" value="a">\n'+
Expand All @@ -297,8 +297,7 @@ test("simple arrays", function(){
equal($("input.array[value='c']", basicForm).is(":checked"), true, "c checked");
equal($("input.array[value='d']", basicForm).is(":checked"), false, "d not checked");
equal(basicForm.jsForm("equals", original), true, "form has not changed");

/*
/*
// update a field
$("input.array[value='a']", basicForm).prop("checked", false);
$("input.array[value='b']", basicForm).prop("checked", true);
Expand All @@ -309,7 +308,60 @@ test("simple arrays", function(){
var pojo = basicForm.jsForm("get");
equal(pojo.checks, ["b", "c"], "b and c checked");
*/
basicForm.remove();

});


test("direct access simple arrays", function(){
// html code for basic form
var basicForm = $('<div>\n'+
'<ul class="collection" data-field="data.steps" id="list1">\n'+
'<li><input type="text" class="number" name="steps." /></li>\n' +
'</ul>\b'+
'<button class="add" data-field="data.steps" id="add1">add</button>\n'+
'<ul class="collection" data-field="data.test.steps" id="list2">\n'+
'<li><input type="text" class="number" name="steps." /></li>\n' +
'</ul>\b'+
'<button class="add" data-field="data.test.steps" id="add2">add</button>\n'+
'</div>');

$("body").append(basicForm);

var original = {
steps: [10,20,30,40,50],
test: {
steps: [100,200,300,400,500]
}
};

// default init: prefix = data
basicForm.jsForm({
data: original
});

equal($("#list1", basicForm).children().length, 5, "5 items in list1");
equal($("#list2", basicForm).children().length, 5, "5 items in list2");
equal(basicForm.jsForm("equals", original), true, "form has not changed");
// add an item in each list
$("#add1", basicForm).click();
$("#add2", basicForm).click();
equal($("#list1", basicForm).children().length, 6, "6 items in list1");
equal($("#list2", basicForm).children().length, 6, "6 items in list2");

$("#list1", basicForm).children().first().find("input").val("1111");
$("#list1", basicForm).children().last().find("input").val("1112");
$("#list2", basicForm).children().first().find("input").val("2221");
$("#list2", basicForm).children().last().find("input").val("2222");
var updated = basicForm.jsForm("get");
equal(updated.steps.length, 6, "6 steps");
equal(updated.steps[0], 1111, "first element: 1111");
equal(updated.steps[5], 1112, "last element: 1112");
equal(updated.test.steps.length, 6, "6 steps in test");
equal(updated.test.steps[0], 2221, "first element: 2221");
equal(updated.test.steps[5], 2222, "last element: 2222");

equal(basicForm.jsForm("equals", original), false, "form has changed");
basicForm.remove();
*/
});

0 comments on commit c10dbf3

Please sign in to comment.