Search in sources :

Example 1 with PublishEvent

use of org.apache.tapestry5.annotations.PublishEvent in project tapestry-5 by apache.

the class PublishServerSideEvents method addEventsAttribute.

private void addEventsAttribute(final Element element) {
    if (element == null) {
        throw new IllegalStateException("@PublishEvent used inside a page which didn't generate a <body> element");
    }
    final ComponentResources containerResources = resources.getContainerResources();
    final ComponentModel componentModel = containerResources.getComponentModel();
    final String metaValue = componentModel.getMeta(InternalConstants.PUBLISH_COMPONENT_EVENTS_META);
    final JSONArray componentEvents = new JSONArray(metaValue);
    final JSONObject events = new JSONObject();
    final String existingValue = element.getAttribute(COMPONENT_EVENTS_ATTRIBUTE_NAME);
    if (existingValue != null) {
        final JSONObject existing = new JSONObject(existingValue);
        for (String key : existing.keys()) {
            events.put(key, existing.get(key));
        }
    }
    for (int i = 0; i < componentEvents.length(); i++) {
        final String eventName = componentEvents.getString(i);
        JSONObject event = new JSONObject();
        event.put(PUBLISH_COMPONENT_EVENTS_URL_PROPERTY, containerResources.createEventLink(eventName).toString());
        events.put(eventName, event);
    }
    element.forceAttributes(TapestryConstants.COMPONENT_EVENTS_ATTRIBUTE_NAME, events.toString());
}
Also used : JSONObject(org.apache.tapestry5.json.JSONObject) ComponentModel(org.apache.tapestry5.model.ComponentModel) JSONArray(org.apache.tapestry5.json.JSONArray) ComponentResources(org.apache.tapestry5.ComponentResources)

Aggregations

ComponentResources (org.apache.tapestry5.ComponentResources)1 JSONArray (org.apache.tapestry5.json.JSONArray)1 JSONObject (org.apache.tapestry5.json.JSONObject)1 ComponentModel (org.apache.tapestry5.model.ComponentModel)1