You create a page local object by calling "cocoon.createPageLocal()". The returned object behaves like a normal JavaScript Object but restores the property values it had when sendPageAndWait() was originally called, each time the page is resubmitted..
With this version the product search function of the Petstore looks like this:
function searchProducts() {
var keyword = cocoon.request.get("keyword");
if (empty(keyword)) {
cocoon.sendPage("view/Error" + EXT, {
accountForm: accountForm,
message: "Please enter a keyword to search for, then press the search button"
});
return;
}
var pageLocal = cocoon.createPageLocal();
pageLocal.skipSearchResults = 0;
var maxSearchResults = 3;
while (true) {
print("skip: " + pageLocal.skipSearchResults);
var result =
getPetStore().searchProductList(keyword,
pageLocal.skipSearchResults,
maxSearchResults);
pageLocal.lastPage = !result.isLimitedByMaxRows;
pageLocal.rowCount = result.rowCount;
cocoon.sendPageAndWait("view/SearchProducts" + EXT, {
accountForm: accountForm,
searchResultsProductList: result.rows,
firstPage: pageLocal.skipSearchResults == 0,
lastPage: pageLocal.lastPage
});
var page = cocoon.request.get("page");
if (page == "previous") {
if (pageLocal.skipSearchResults != 0) {
pageLocal.skipSearchResults -= maxSearchResults;
}
} else if (page == "next") {
if (!pageLocal.lastPage) {
pageLocal.skipSearchResults += pageLocal.rowCount;
}
}
}
}
Source code attached.
One thing to beware of with this approach, however, is memory growth, as each continuation has its own copy of the page local variables.
WDYT?
Chris
pageLocal.zip
Description: Zip compressed data