Taking Sylvain's idea of ContinuationLocal variables, I prototyped an implementation. Since the continuation local variable must be associated with the object managing the actual continuations, I made FOM_Cocoon the factory for creating these (but another object could provide this role). I've called these variables "PageLocal" rather than ContinuationLocal as in this case they are associated with the continuation of sendPageAndWait(), and not with any arbitrary continuation

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

Attachment: pageLocal.zip
Description: Zip compressed data

Reply via email to