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(
url="",
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>