Monday, June 8, 2015
Sunday, June 7, 2015
JavaScript - iterate or loop over JavaScript object or JSON
It is taken from here: http://stackoverflow.com/questions/684672/loop-through-javascript-object
In JavaScript, every object has a bunch of built-in key-value pairs that have meta-information. When you loop through all the key-value pairs for an object you're looping through them too. hasOwnPropery() filters these out:
In JavaScript, every object has a bunch of built-in key-value pairs that have meta-information. When you loop through all the key-value pairs for an object you're looping through them too. hasOwnPropery() filters these out:
for (var key in p) {
if (p.hasOwnProperty(key)) {
alert(key + " -> " + p[key]);
}
}
In ECMAScript 5 you have new approach in iteration fields of literal -
Object.keys
More information you can see on MDN
My choice is below as a faster solution in current versions of browsers (Chrome30, IE10, FF25)
var keys = Object.keys(p),
len = keys.length,
i = 0,
prop,
value;
while (i < len) {
prop = keys[i];
value = p[prop];
i += 1;
}
Friday, June 5, 2015
GWTBootstrap - custom email validation for TextBox
=== JAVA CODE ===
TextBox emailLookup = new TextBox();
emailLookup.addValidator(new Validator() {
@Override
public int getPriority() {
return 0;
}
@Override
public List<EditorError> validate(Editor editor, Object value) {
List<EditorError> result = new ArrayList<EditorError>();
String valueStr = value == null ? "" : value.toString();
if (!valueStr.matches(EMAIL_REGEXP)) {
result.add(new BasicEditorError(emailLookup, value, "Not a valid email address"));
} else if (isEmailReapeated(valueStr)) {
result.add(new BasicEditorError(emailLookup, value, "Email address is repeated"));
}
return result;
}
});
=== UI BINDER ===
<b:Row addStyleNames="{style.padding}">
<b:Column size="XS_12">
<b:Row>
<b:Column size="XS_2">
<b:FormGroup>
<b:TextBox ui:field="emailLookup" placeholder="Enter email" allowBlank="false"/>
<b:InlineHelpBlock iconType="EXCLAMATION"/>
</b:FormGroup>
</b:Column>
<b:Column size="XS_1">
<b:Button ui:field="addContact" text="Add"/>
</b:Column>
</b:Row>
</b:Column>
</b:Row>
TextBox emailLookup = new TextBox();
emailLookup.addValidator(new Validator() {
@Override
public int getPriority() {
return 0;
}
@Override
public List<EditorError> validate(Editor editor, Object value) {
List<EditorError> result = new ArrayList<EditorError>();
String valueStr = value == null ? "" : value.toString();
if (!valueStr.matches(EMAIL_REGEXP)) {
result.add(new BasicEditorError(emailLookup, value, "Not a valid email address"));
} else if (isEmailReapeated(valueStr)) {
result.add(new BasicEditorError(emailLookup, value, "Email address is repeated"));
}
return result;
}
});
=== UI BINDER ===
<b:Row addStyleNames="{style.padding}">
<b:Column size="XS_12">
<b:Row>
<b:Column size="XS_2">
<b:FormGroup>
<b:TextBox ui:field="emailLookup" placeholder="Enter email" allowBlank="false"/>
<b:InlineHelpBlock iconType="EXCLAMATION"/>
</b:FormGroup>
</b:Column>
<b:Column size="XS_1">
<b:Button ui:field="addContact" text="Add"/>
</b:Column>
</b:Row>
</b:Column>
</b:Row>
Subscribe to:
Posts (Atom)