Suggestion: event.paramValue()

14 views
Skip to first unread message

Rawlins

unread,
Dec 19, 2011, 7:11:02 AM12/19/11
to model...@googlegroups.com
Hello Guys,

I'm finding myself with an increasing need to param a value into the event (set it only if it does not already exist).

One example use case for this is form validation.

In my controller method for the form I need a validation result object, which contains all the errors and other failure information.

This object could come from two places - It could be that the user has been passed back to this form page after a failed validation, with the result object preserved in the event, or if one doesn't already exist and this is the first time viewing the form, I'll create a new result object for them.

This results in handler code that looks something like this:

<!--- Param the validation result into the object too. --->
<cfif NOT event.valueExists("ValidationResult")>
<cfset event.setValue("ValidationResult", Beans.ValidateThis.newResult()) />
</cfif>

This works just fine, but it's needlessly verbose. I'd like to propose a change to the event object API that makes this much easier - here are a couple of ideas:

event.paramValue('name', 'value');

or a modification to the current setValue() method, which adds a 3rd argument that defines if the value should be overwritten, if false and a previous value exists for the key then it's left intact.

event.setValue("name", "value", false);

Does anyone else have some ideas on this? am I looking at this situation in completely the wrong way? does something already exist like this?

Thanks.

Robert

Dan Wilson

unread,
Dec 19, 2011, 9:11:34 AM12/19/11
to model...@googlegroups.com
I think the event.getValue() method sort of does this.




DW

--
Model-Glue Sites:
Home Page: http://www.model-glue.com
Documentation: http://docs.model-glue.com
Bug Tracker: http://bugs.model-glue.com
Blog: http://www.model-glue.com/blog
 
You received this message because you are subscribed to the Google
Groups "model-glue" group.
To post to this group, send email to model...@googlegroups.com
To unsubscribe from this group, send email to
model-glue+...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/model-glue?hl=en



--
Plutarch - "The mind is not a vessel to be filled but a fire to be kindled."

Robert Rawlins

unread,
Dec 19, 2011, 9:25:46 AM12/19/11
to model...@googlegroups.com
Hi Dan,

You're quite right - it does sort of do it. I use the getValue default argument on a regular basis within my views - but I hadn't realised that it would set the value too if it didn't already exist before returning it.

This will certainly do the job - it just looks a little odd from a semantic point of view within the controller. Calling get when what I'm really intending to do it set.

Given that it's a simple implementation do you think there is a case for adding a new method to the API as I suggested? It would make code within the controller more meaningful if I was calling pramValue() I think.

Robert

Chris Blackwell

unread,
Dec 19, 2011, 9:35:40 AM12/19/11
to model...@googlegroups.com
Its really easy to use a customised EventContext.

create component that extends ModelGlue.gesture.eventrequest.EventContext
add your paramValue method to it
add this to your apps coldspring.xml

<bean id="modelglue.eventContextFactory" class="ModelGlue.gesture.eventrequest.EventContextFactory">
<property name="modelGlue"><ref bean="modelglue.ModelGlue"/></property>
<property name="statePersister"><ref bean="modelglue.statePersister"/></property>
<property name="viewRenderer"><ref bean="modelglue.viewRenderer"/></property>
<property name="beanPopulator"><ref bean="modelglue.beanPopulator"/></property>
<property name="logWriter"><ref bean="modelglue.logWriter"/></property>

<!-- path to your EventContext in here -->
<property name="objectPath"><value>com.myapp.myfancyeventcontext</value></property>
</bean>

Chris

Dan Wilson

unread,
Dec 19, 2011, 9:40:06 AM12/19/11
to model...@googlegroups.com
It would be simple to add in Robert, but I'm not sure what it adds over the current implementation (That's been in there since V1).

The way I read this in my head is:

event.getValue("Foo", "Bar");

Gimme the value of Foo in the event, and if you don't have one, set event.Foo to bar and gimme Foo.

DW

Robert Rawlins

unread,
Dec 19, 2011, 9:51:31 AM12/19/11
to model...@googlegroups.com
Thanks Guys,

@Chris - I didn't realize I could extend the event context - that's really great (have a bunch of ideas I can implement with that)

@Dan - That's fair enough - if you don't feel there is a benefit then it's no problem. I shall use Chris's suggested approach.

Robert

Robert Rawlins

unread,
Dec 19, 2011, 10:19:35 AM12/19/11
to model...@googlegroups.com
Hi Chris,

I've put together my own context object, and added the config bean as you've suggested, below my config for modelglue.modelGlueConfiguration

However, the app still seems to be using the standard event context.

Any ideas?

Thanks.

Robert

Matt Quackenbush

unread,
Dec 19, 2011, 10:21:28 AM12/19/11
to model...@googlegroups.com
Reload the app?
Double check the path?

I know, I know. Captain Obvious to the rescue.

Robert Rawlins

unread,
Dec 19, 2011, 11:37:10 AM12/19/11
to model...@googlegroups.com
Hi Matt,

I had done both of those things before posting, always worth double double checking.

I can create an instance of my custom event context manually using createObject() so I can be sure the path I'm using, and the component itself are both fine.

Just seems that MG isn't picking it up.

Thanks.

Robert

Chris Blackwell

unread,
Dec 19, 2011, 12:03:48 PM12/19/11
to model...@googlegroups.com
Hi Rob,

Are you using MG3?  

I'm not sure exactly when the modelglue.eventContextFactory bean came into existence, but if you look in /ModelGlue/gesture/configuration/ModelGlueConfiguration.xml and its there then you can override it.

Also try clearing your cfc cache.  If you're on Railo, i find sometimes it holds on to compiled cfc's even after changes. Delete /WEB-INF/railo/cfclasses/* to force it to recompile everything.

Chris

Robert Rawlins

unread,
Dec 19, 2011, 12:19:34 PM12/19/11
to model...@googlegroups.com
Thanks Chris,

This is the latest MG. I checked the xml and indeed the definition is in there.

I have managed to solve my problem - although I'm not quite sure why.

I moved the definition for my custom context from.

/config/coldspring/modelglue.xml

to 

/config/coldspring/beans.xml

so instead of being sat along with my model glue config it's now sat with all my other app beans.

Strange - think it might be to do with parent bean factory configuration.

Rob
Reply all
Reply to author
Forward
0 new messages