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.ioc.annotations.Inject;
024
025/**
026 * This mixin triggers event notifcations to identify when it enters
027 * the {@link BeginRender} and {@link AfterRender} render phases.
028 * The {@link MarkupWriter} is passed as the event context. The most common use of this
029 * is to handle the "afterRender" event to generate client-side JavaScript for content
030 * just rendered via a {@link Block} (this is a common Ajax use case related to partial
031 * page rendering).
032 * 
033 * @since 5.2.0
034 * @tapestrydoc
035 */
036@Events(
037{ "beginRender", "afterRender" })
038public class RenderNotification
039{
040    @Inject
041    private ComponentResources resources;
042
043    void beginRender(MarkupWriter writer)
044    {
045        trigger(writer, "beginRender");
046    }
047
048    void afterRender(MarkupWriter writer)
049    {
050        trigger(writer, "afterRender");
051    }
052
053    private void trigger(MarkupWriter writer, String eventName)
054    {
055        resources.triggerEvent(eventName, new Object[]
056        { writer }, null);
057    }
058}