}
AuraContext context = getContext("is");
try {
DefinitionService definitionService = Aura.getDefinitionService();
DefDescriptor<ComponentDef> descriptor = definitionService.getDefDescriptor(tag,
ComponentDef.class);
Map<String, Object> actionAttributes = Maps.newHashMap();
Map<String, String> actionEventHandlers = Maps.newHashMap();
ComponentDef componentDef = descriptor.getDef();
if(attributes!=null) {
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
String key = entry.getKey();
AttributeDef attributeDef = componentDef.getAttributeDef(key);
if (attributeDef != null) {
String name = attributeDef.getName();
actionAttributes.put(name, entry.getValue());
} else {
RegisterEventDef eventDef = componentDef.getRegisterEventDefs().get(key);
if (eventDef != null) {
// Emit component.addHandler() wired to special global scope value provider
String name = eventDef.getAttributeName();
actionEventHandlers.put(name, (String) entry.getValue());
} else {
throw new AuraRuntimeException(String.format("Unknown attribute or event %s - %s", tag, key));
}
}
}
}
try {
StringBuilder jsonEventHandlers = null;
if (!actionEventHandlers.isEmpty()) {
// serialize registered event handlers into js object
jsonEventHandlers = new StringBuilder();
Json.serialize(actionEventHandlers, jsonEventHandlers);
}
StringBuilder init = new StringBuilder();
if (useAsync) {
// uses newComponentAsync to create component
StringBuilder jsonAttributes = new StringBuilder();
Json.serialize(actionAttributes, jsonAttributes);
// set event handlers to either js "undefined" or object of event and handler names
String eventHandlers = jsonEventHandlers != null ? jsonEventHandlers.toString() : "undefined";
String def = String.format(COMPONENT_DEF_TEMPLATE, tag, jsonAttributes.toString(), localId);
String newComponentScript = String.format(ASYNC_INJECTION_TEMPLATE, def, locatorDomId, eventHandlers);
init.append(newComponentScript);
} else {
// config printed onto HTML page
// mark injectee component as loaded
// only when not using async because component defs will be printed onto HTML
definitionService.updateLoaded(descriptor);
ControllerDef componentControllerDef = definitionService.getDefDescriptor("aura://ComponentController",
ControllerDef.class).getDef();
Map<String, Object> paramValues = Maps.newHashMap();
paramValues.put("name", descriptor.getQualifiedName());
paramValues.put("attributes", actionAttributes);