You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by wh...@mac.com on 2003/06/30 18:54:28 UTC

Refactoring Flow/XMLForms example

I'm continuing to play with Flow and in particular, XMLForms:

Today I decided to refactor the flow-driven feedback wizard in the  
samples:

http://localhost:8080/cocoon/samples/xmlform/flow

In particular, I wanted to get rid of the inlined validation function  
definitions in the calls to Xform.sendview ().

So I went into feedbackWizard.js and defined a userinfo object with a  
member, bean, and the two validation functions defined as methods.

However when I restarted Cocoon, it looks like XForm.sendView is not  
getting the validation function  passed to it, as when I enter data  
that would trigger the validator, it's ignored, and only the Schematron  
validation's applied.

I know that passing a function as data in JavaScript's a little  
non-obvious.

----
Here's my revisions to feedbackWizard.js

// XML Form Feedback Wizard Application

cocoon.load("resource://org/apache/cocoon/components/flow/javascript/ 
xmlForm.js");

/**
  * Constructor for UserInfo object
  */
function UserInfo ()
{
	this.bean = null;
}

/**
  * Validate UserIdentity
  * @param xform XForm object
  */
function UserInfo_validateUserIdentity (xform)
{
	var bean = xform.getModel();
	print("I can also do validation in JavaScript");
	print("age = "+xform.getValue("number(/age)"));
	print("role = "+bean.role);
	if (bean.age > 40) {
		xform.addViolation("/age", "Hey, you're too old");
	}
}

UserInfo.prototype.validateUserIdentity = function (xform)
{
	UserInfo_validateUserIdentity (xform);
}

/**
  * Validate Deployment
  * @param xform XForm object
  */
function UserInfo_validateDeployment (xform)
{
	var bean = xform.getModel();
	print("I can also do validation in JavaScript");
	if (bean.publish) {
		xform.addViolation("/publish", "Sorry, I won't let you publish");
	}
}

UserInfo.prototype.validateDeployment = function (xform)
{
	UserInfo_validateDeployment (xform);
}

function feedbackWizard(xform) {
	var userinfo = new UserInfo ();
     userinfo.bean = {
         firstName: "Donald",
         lastName: "Duck",
         email: "donald_duck@disneyland.com",
         age: 5,
         number: 1,
         liveUrl: "http://",
         publish: true,
         hidden: true,
         count: 1,
         notes: "<your notes here>",
         favorite: ["http://xml.apache/org/cocoon",
                    "http://jakarta.apache.org",
                    "http://www.google.com",
                    "http://www.slashdot.com",
                    "http://www.yahoo.com"],
         hobby: ["swim", "movies", "ski", "gym", "soccer"],
         allHobbies: [
             {
                 key: "swim",
                 value: "Swimming"
             },
             {
                 key: "gym",
                 value: "Body Building"
             },
             {
                 key: "ski",
                 value: "Skiing"
             },
             {
                 key: "run",
                 value: "Running"
             },
             {
                 key: "football",
                 value: "Football"
             },
             {
                 key: "read",
                 value: "Reading"
             },
             {
                 key: "write",
                 value: "Writing"
             },
             {
                 key: "soccer:",
                 value: "Soccer"
             },
             {
                 key: "blog",
                 value: "Blogging"
             }],
         role: ["Hacker", "Executive"],
         system: {
             os: "Unix",
             processor: "p4",
             ram: 512,
             servletEngine: "Tomcat",
             javaVersion: "1.3",
         }
     }

     xform.setModel(userinfo.bean);

     xform.sendView("userIdentity",
                    "userIdentity.xml",
                    userinfo.validateUserIdentity (xform));

// move this into a method of UserInfo

/*                    function(xform) { */
/*        var bean = xform.getModel(); */
/*         print("I can also do validation in JavaScript"); */
/*         print("age = "+xform.getValue("number(/age)")); */
/*         print("role = "+bean.role); */
/*        if (bean.age > 40) { */
/*             xform.addViolation("/age", "Hey, you're too old"); */
/*         } */
/*     }); */
     print("handling user identity");

     xform.sendView("deployment",
                    "deployment.xml",
                    userinfo.validateDeployment (xform));

// move this into a method of UserInfo

/*                    function(xform) { */
/*         var bean = xform.getModel(); */
/*         print("I can also do validation in JavaScript"); */
/*        if (bean.publish) { */
/*            xform.addViolation("/publish", "Sorry, I won't let you  
publish"); */
/*         } */
/*     }); */
     print("handling deployment");

     xform.sendView("system", "system.xml");
     print("handling system");

     xform.sendView("confirm", "confirm.xml");
     print("handling confirm");

     xform.finish("end.xml");
     print("done");
}


-- whump


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


Re: Refactoring Flow/XMLForms example

Posted by wh...@mac.com.
Further experiment and was able to answer my question:

On Monday, June 30, 2003, at 09:54  AM, whump@mac.com wrote:

> I'm continuing to play with Flow and in particular, XMLForms:
>
> Today I decided to refactor the flow-driven feedback wizard in the 
> samples:
>
> http://localhost:8080/cocoon/samples/xmlform/flow
>
> In particular, I wanted to get rid of the inlined validation function 
> definitions in the calls to Xform.sendview ().

The solution is that while you still have to call the function wrapper 
in the arguments, you can define your functions elsewhere thus:

> function UserInfo_validateUserIdentity (xform)
> {
> 	var bean = xform.getModel();
> 	print("I can also do validation in JavaScript");
> 	print("age = "+xform.getValue("number(/age)"));
> 	print("role = "+bean.role);
> 	if (bean.age > 40) {
> 		xform.addViolation("/age", "Hey, you're too old");
> 	}
> }
>
> UserInfo.prototype.validateUserIdentity = function (xform)
> {
> 	UserInfo_validateUserIdentity (xform);
> }
...
>     xform.sendView("userIdentity",
>                    "userIdentity.xml",
                   function (xform) { userinfo.validateUserIdentity 
(xform); });

and now the refactored example works as before.

-- whump


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