Dashboard > RIFE > ... > Web engine > Stateful components
RIFE Log In | Sign Up   View a printable version of the current page.
Stateful components


Added by Geert Bevin, last edited by Geert Bevin on Jul 31, 2006
Labels: 
(None)

RIFE makes it very easy for element instances to indicate which state needs to be preserved for them (in case you don't want to use continuations for this).

You simply create datalinks that point back to exactly the same element as the one they originate from. These are called reflexive datalinks. The data that's available through the connected outputs will be collected by RIFE and provided as inputs to the element when it's processed the next time. Note, this seperates out the collected data for each individual element instance you use on a site, even for embedded elements. While this is the behavior you would expect, it's worth pointing out that the data is not preserved globally for all the elements of the same type.

Let's look at the same counter example that we used to demonstrate parallel continuations, only this time it uses stateful components (you can try this version out online).

The element implementation Counter.java:

@Elem(
// Setting an empty URL, makes the submissions target the element that embeds
// the embedded Counter elements. Without this declaration, each embedded
// element would become the main element after the submission (since it has
// its own URL).
url="",
// This data link connects the 'counter' output to the 'counter' value. The
// element might have changed the counter property value in the meantime
// (after an 'increase' or 'decrease' submission). The reflective datalink
// will pass the output value to the input at the next submission.
datalinks = {@Datalink(srcOutput="counter", destInput="counter", destClass=Counter.class)}
)
public class Counter extends Element {
    private int counter;
    @InputProperty  public void setCounter(int counter) { this.counter = counter; }
    @OutputProperty public int getCounter() { return counter; }
     
    @SubmissionHandler
    public void doDecrease() {
        counter--;
        processElement();
    }
     
    @SubmissionHandler
    public void doIncrease() {
        counter++;
        processElement();
    }
     
    public void processElement() {
        Template t = getHtmlTemplate("counter");
        t.setValue("counter", counter);
        print(t);
    }
}

The counter.html template that is used by the element above:

<div>
    <r:v name="counter"/>
     
    <form action="${v SUBMISSION:FORM:decrease/}" method="post">
        <r:v name="SUBMISSION:PARAMS:decrease"/>
        <input type="submit" value=" - " />
    </form>
     
    <form action="${v SUBMISSION:FORM:increase/}" method="post">
        <r:v name="SUBMISSION:PARAMS:increase"/>
        <input type="submit" value=" + " />
 
    </form>
</div>

The main template that includes three stateful counters as embedded elements:

<body>
    <r:v name="ELEMENT:Counter:1"/>
    <br />
    <r:v name="ELEMENT:Counter:2"/>
    <br />
    <r:v name="ELEMENT:Counter:3"/>
</body>



Are you enjoying Confluence? Please consider purchasing it today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.1a Build:#515 May 19, 2006) - Bug/feature request - Contact Administrators