cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jason Johnston <coc...@lojjic.net>
Subject Re: Redirect at the end of a form
Date Mon, 04 Jun 2007 12:22:06 GMT
Tobia wrote:
> Cocoon users,
> 
> I would like to get your feedback on a piece of flowscript I just wrote
> to solve what I believe to be a common problem.
> 
> Suppose you have a form that asks the user for some data, validates it
> and inserts it into a database, using a DB access layer for flowscript,
> such as jdbi.js from the samples.  The basic flowscript would be:
> 
> 	// create and show the form
> 	var form = new Form(...)
> 	form.showForm(...)
> 
> 	/* execute DB operations to store the newly inserted data */
> 
> 	//send a results page back to the user
> 	cocoon.sendPage(...)
> 
> The final sendPage() sends a custom page to show the user what he has
> just inserted.  Suppose also that said data does not have a public url,
> but is only available to the submitting user at the end of the form.
> 
> What is the problem with the previous flowscript?  If the user reloads
> the final page, the DB operations are executed again, possibly resulting
> in duplicate insertions or other errors.
> 
> The usual solution to this problem is to issue a client-side redirect to
> a GET url, so that subsequent client reloads don't have side effects.

I think there's a simpler way to avoid the double insertions, also using 
continuations but without a client-side redirect:

--------------------------------------
var processed = false;

// create and show the form
var form = new Form(...);
form.showForm(...);

if(!processed) {
     /* execute DB operations to store the newly inserted data */
     processed = true;
}

//send a results page back to the user
cocoon.sendPage(...);
--------------------------------------

When the user hits refresh on the confirmation page, the database 
operations will be skipped and the confirmation displayed again.

WDYT?

> 
> Continuations seem the perfect tool to issue such a redirect to a
> private url built on-the-fly for just one user, but I couldn't find an
> idiomatic way of doing it in the samples, so I wrote my own.
> 
> The following function is called after the DB operations, just before
> the final sendPage()
> 
> 	function redirectToGet() {
> 	  var done = false
> 	  var cont = cocoon.createWebContinuation()
> 	  if (! done) {
> 	    done = true
> 	    var url = /* build a url that will call cont.id */
> 	    cocoon.redirectTo(url, true)
> 	  }
> 	}
> 
> What do you think?  Is there a better way of doing it?  How is this
> problem usually solved?  Any feedback is welcome.
> 
> 
> Tobia
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Mime
View raw message