Skip to content

Commit

Permalink
Fixes to array field and address field
Browse files Browse the repository at this point in the history
  • Loading branch information
uzquiano committed May 15, 2014
1 parent 05d8a92 commit 8f857ee
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 37 deletions.
94 changes: 59 additions & 35 deletions js/fields/basic/ArrayField.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,24 @@
/**
* @see Alpaca.ContainerField#setup
*/
setup: function() {
setup: function()
{
this.base();

this.options.toolbarStyle = Alpaca.isEmpty(this.view.toolbarStyle) ? "button" : this.view.toolbarStyle;
if ( !Alpaca.isEmpty(this.parent.options.form.attributes.rubyrails) )
{
if ( this.parent.options.form.attributes.rubyrails == "true" ) {
this.options.rubyrails = true;
} else {
this.options.rubyrails = false;
}
} else {
this.options.rubyrails = false;
}

if (!this.options.items) {

// determine whether we are using "ruby on rails" compatibility mode
this.options.rubyrails = false;
if (this.parent && this.parent.options && this.parent.options.form && this.parent.options.form.attributes)
{
if (!Alpaca.isEmpty(this.parent.options.form.attributes.rubyrails))
{
this.options.rubyrails = true;
}
}

if (!this.options.items)
{
this.options.items = {};
}

Expand Down Expand Up @@ -76,13 +77,18 @@
});
}

if (Alpaca.isEmpty(this.data)) {
if (Alpaca.isEmpty(this.data))
{
return;
}
if (!Alpaca.isArray(this.data)) {
if (!Alpaca.isString(this.data)) {
if (!Alpaca.isArray(this.data))
{
if (!Alpaca.isString(this.data))
{
return;
} else {
}
else
{
try {
this.data = Alpaca.parseJSON(this.data);
if (!Alpaca.isArray(this.data)) {
Expand All @@ -104,16 +110,21 @@

var _this = this;

if (!data || !Alpaca.isArray(data)) {
if (!data || !Alpaca.isArray(data))
{
return;
}

// set fields
for (var i = 0; i < this.children.length; i++) {
for (var i = 0; i < this.children.length; i++)
{
var childField = this.children[i];
if (data.length > i) {
if (data.length > i)
{
childField.setValue(data[i]);
} else {
}
else
{
this.removeItem(childField.id); //remove child items if there are more children than in data
}
}
Expand Down Expand Up @@ -157,7 +168,6 @@
});
});
}

},

/**
Expand Down Expand Up @@ -219,9 +229,11 @@
/**
* Update field path and name when an array item is removed, inserted or switched.
*/
updatePathAndName: function() {
updatePathAndName: function()
{
var _this = this;
if (this.children) {
if (this.children)
{
$.each(this.children,function(i,v) {
var idx = v.path.lastIndexOf('/');
var lastSegment = v.path.substring(idx+1);
Expand All @@ -234,22 +246,34 @@

}
// re-calculate name
if (v.nameCalculated) {
if (v.nameCalculated)
{
v.preName = v.name;
if (v.parent && v.parent.name && v.path) {

if (v.parent && v.parent.name && v.path)
{
v.name = v.parent.name + "_" + i;
} else {
if (v.path) {
}
else
{
if (v.path)
{
v.name = v.path.replace(/\//g, "").replace(/\[/g, "_").replace(/\]/g, "");
}
}
if (this.parent.options.rubyrails ) {
$(v.field).attr('name', v.parent.name);
} else {
$(v.field).attr('name', v.name);
}

if (this.parent.options.rubyrails )
{
$(v.field).attr('name', v.parent.name);
}
else
{
$(v.field).attr('name', v.name);
}
}
if (!v.prePath) {

if (!v.prePath)
{
v.prePath = v.path;
}
_this.updateChildrenPathAndName(v);
Expand Down
4 changes: 2 additions & 2 deletions tests/js/fields/AddressField.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
equal(selectOptionElems.length, 60, 'Address state select field generated correctly.');
var zipElem = $('.alpaca-fieldset-items-container input[name="zip"]', inputElem);
ok(zipElem.length, 'Address zip field generated correctly.');
equal(zipElem.attr('size'), 5, 'Address zip field generated with right size.');
equal(parseInt(zipElem.attr('size')), 5, 'Address zip field generated with right size.');
zipElem.focus(function() {
equal(zipElem.val(), "_____", "Address zip field text mask generated correctly.");
start();
});
zipElem.val('');
zipElem.focus();
start();
}
});
});
Expand Down

0 comments on commit 8f857ee

Please sign in to comment.