    001// Copyright 2010, 2011 The Apache Software Foundation
    002//
    003// Licensed under the Apache License, Version 2.0 (the "License");
    004// you may not use this file except in compliance with the License.
    005// You may obtain a copy of the License at
    006//
    007// http://www.apache.org/licenses/LICENSE-2.0
    008//
    009// Unless required by applicable law or agreed to in writing, software
    010// distributed under the License is distributed on an "AS IS" BASIS,
    011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012// See the License for the specific language governing permissions and
    013// limitations under the License.
    014
    015package org.apache.tapestry5.corelib.mixins;
    016
    017import org.apache.tapestry5.Block;
    018import org.apache.tapestry5.ComponentResources;
    019import org.apache.tapestry5.MarkupWriter;
    020import org.apache.tapestry5.annotations.AfterRender;
    021import org.apache.tapestry5.annotations.BeginRender;
    022import org.apache.tapestry5.annotations.Events;
    023import org.apache.tapestry5.annotations.MixinAfter;
    024import org.apache.tapestry5.ioc.annotations.Inject;
    025
    026/**
    027 * This mixin triggers event notifcations to identify when it enters
    028 * the {@link BeginRender} and {@link AfterRender} render phases.
    029 * The {@link MarkupWriter} is passed as the event context. The most common use of this
    030 * is to handle the "afterRender" event to generate client-side JavaScript for content
    031 * just rendered via a {@link Block} (this is a common Ajax use case related to partial
    032 * page rendering).
    033 *
    034 * @since 5.2.0
    035 * @tapestrydoc
    036 */
    037@Events(
    038{ "beginRender", "afterRender" })
    039@MixinAfter
    040public class RenderNotification
    041{
    042    @Inject
    043    private ComponentResources resources;
    044
    045    void beginRender(MarkupWriter writer)
    046    {
    047        trigger(writer, "beginRender");
    048    }
    049
    050    void afterRender(MarkupWriter writer)
    051    {
    052        trigger(writer, "afterRender");
    053    }
    054
    055    private void trigger(MarkupWriter writer, String eventName)
    056    {
    057        resources.triggerEvent(eventName, new Object[]
    058        { writer }, null);
    059    }
    060}