Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for 
change notification.

The following page has been changed by NickWestgate:
http://wiki.apache.org/tapestry/Tapestry5SubmitContextComponent

New page:
This is especially useful in loops, where the component name is dynamic.[[BR]]

To use it, add this to your page template:
{{{
<input t:type="SubmitContext" t:id="theSubmit" t:context="idString"/>
}}}

add this to your page class:
{{{
void onSelectedFromTheSubmit(String idString)
{
    // do something based on idString
}
}}}

'''Source:'''[[BR]]
(put this component class into yourApp.components package)
{{{
// Copyright 2007 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package your.package.tapestry.components;

import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.MarkupWriter;
import org.apache.tapestry.annotations.Environmental;
import org.apache.tapestry.annotations.Inject;
import org.apache.tapestry.annotations.Parameter;
import org.apache.tapestry.corelib.base.AbstractField;
import org.apache.tapestry.services.FormSupport;
import org.apache.tapestry.services.Heartbeat;
import org.apache.tapestry.services.Request;

/**
  * Corresponds to &lt;input type="submit"&gt;, a client-side element that can 
force the
  * enclosing form to submit. The submit responsible for the form submission 
will post a
  * notification that allows the application to know that it was the 
responsible entity. The
  * notification is named "selected" and has a String context.
  */
public final class SubmitContext extends AbstractField
{
     static final String SELECTED_EVENT = "selected";

     /**
      * If true, then any notification sent by the component will be deferred 
until the end of
      * the form submission (this is usually desirable).
      */
      <at> Parameter
     private boolean _defer = true;

      <at> Parameter
     private String _context;

      <at> Environmental
     private FormSupport _formSupport;

      <at> Environmental
     private Heartbeat _heartbeat;

      <at> Inject
     private ComponentResources _resources;

      <at> Inject
     private Request _request;

     public SubmitContext()
     {
     }

     SubmitContext(Request request)
     {
         _request = request;
     }

     void beginRender(MarkupWriter writer)
     {
         // write a hidden input for the context
         String elementName = getElementName();
         writer.element("input", "type", "hidden", "name", elementName + "X", 
"value", _context);
         writer.end();

         // now the submit
         writer.element("input", "type", "submit", "name", elementName, "id", 
getClientId());
     }

     void afterRender(MarkupWriter writer)
     {
         writer.end();
     }

      <at> Override
     protected void processSubmission(FormSupport formSupport, String 
elementName)
     {
         String value = _request.getParameter(elementName);
         final String context = _request.getParameter(elementName + "X");

         if (value == null)
             return;

         Runnable sendNotification = new Runnable()
         {
             public void run()
             {
                 _resources.triggerEvent(SELECTED_EVENT, new Object[] 
{context}, null);
             }
         };

         // When not deferred, don't wait, fire the event now (actually, at the 
end of the current
         // heartbeat). This is most likely because the Submit is inside a Loop 
and some contextual
         // information will change if we defer. Another option might be to 
wait until the next
         // heartbeak?

         if (_defer)
             _formSupport.defer(sendNotification);
         else
             _heartbeat.defer(sendNotification);

     }

     // For testing:

     void setDefer(boolean defer)
     {
         _defer = defer;
     }

     void setup(ComponentResources resources, FormSupport support, Heartbeat 
heartbeat)
     {
         _resources = resources;
         _formSupport = support;
         _heartbeat = heartbeat;
     }
}
}}}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to