Here are some samples of how you might fetch data for a single product across three related events in each framework. Each example assumes that the model is the same and that information for the individual Product is returned as a bean from an Application.ProductService object via a getProduct() method that accepts one productid argument. The affected code is highlighted in each example:
<cffunction name="needProduct"> <cfargument name="event" required="true" /> <cfset var rc = event.getCollection() /> <cfparam name="rc.productid" default="" /> <cfset rc.product = Application.ProductService.getProduct(rc.productid) /> </cffunction> <cffunction name="productDetail"> <cfargument name="event" required="true" /> <cfset needProduct(event) /> <cfset event.setView("product/detail") /> </cffunction> <cffunction name="productEdit"> <cfargument name="event" required="true" /> <cfset needProduct(event) /> <cfset event.setView("product/form") /> </cffunction> <cffunction name="productSave"> <cfargument name="event" required="true" /> <cfset var rc = event.getCollection() /> <cfset needProduct(event) /> ... save the product ... <cfset setNextEvent("Handler.Event"[,"query_string=parameters"]) /> </cffunction>
<fuseaction name="getProduct" /> <set name="attributes.productid" value="" overwrite="false" /> <set name="product" value="#application.ProductService.getProduct(attributes.productid)#" /> </fuseaction>
<fuseaction name="productDetail"> <do action="storeModel.getProduct" /> <do action="storeView.productAdmin" /> </fuseaction> <fuseaction name="productEdit"> <do action="storeModel.getProduct" /> <do action="storeView.productEdit" /> </fuseaction> <fuseaction name="productSave"> <do action="storeModel.getProduct" /> <do action="storeView.productSave" /> </fuseaction>
FuseBox (CFC): only minor differences from ColdBox above
<cfcomponent output="false">
<cffunction name="configure" output="false">
... configure the listener ...
</cffunction>
<cffunction name="getProduct" access="public" returntype="any" output="false">
<cfargument name="event" type="MachII.framework.Event" required="true" />
<cfreturn application.ProductService.getProduct(event.getArg("productID")) />
</cffunction>
</cfcomponent>
<!DOCTYPE mach-ii PUBLIC "-//Mach-II//DTD Mach-II Configuration 1.5.0//EN" "http://www.mach-ii.com/dtds/mach-ii_1_5_0.dtd" > <mach-ii version="1.5"> <listeners> <listener name="storeListener" type="mii.listeners.storeListener" /> </listeners> <event-handlers> <event-handler event="productAdmin" access="public"> <notify listener="storeListener" method="getProduct" resultArg="product" /> <view-page name="productAdmin" /> </event-handler> <event-handler event="productEdit" access="public"> <notify listener="storeListener" method="getProduct" resultArg="product" /> <view-page name="productForm" /> </event-handler> <event-handler event="productAdmin" access="public"> <notify listener="storeListener" method="getProduct" resultArg="product" /> <redirect url="index.cfm?event=productAdmin" persistArgs="productid" /> </event-handler> </event-handlers> </mach-ii>
<cfcomponent displayname="store" extends="ModelGlue.unity.controller.Controller" output="false">
<cffunction name="getProductFromService" access="private" output="false">
<cfargument name="productid" type="string" required="true" />
<cfreturn Application.ProductService.getProduct(arguments.productID) />
</cffunction>
<cffunction name="getProduct" access="public" output="false">
<cfargument name="event" type="any">
<cfset event.setValue("product",getProductFromService(event.getValue("productid"))) />
</cffunction>
<cffunction name="saveProduct" access="public" output="false">
<cfargument name="event" type="any">
<cfset var product = getProductFromService(event.getValue("productid")) />
... save the product ...
</cffunction>
</cfcomponent>
<modelglue> <controllers> <controller name="store" type="glu.controller.store"> <message-listener message="needProduct" function="getProduct" /> <message-listener message="saveProduct" function="saveProduct" /> </controller> </controllers> <event-handlers> <event-handler name="productAdmin"> <broadcasts> <message name="needProduct" /> </broadcasts> <views> <include name="body" template="product/admin.cfm" /> </views> </event-handler> <event-handler name="productAdmin"> <broadcasts> <message name="needProduct" /> </broadcasts> <views> <include name="body" template="product/admin.cfm" /> </views> </event-handler> <event-handler name="productAdmin"> <broadcasts> <message name="saveProduct" /> </broadcasts> <views> <include name="body" template="product/admin.cfm" /> </views> </event-handler> </event-handlers> </modelglue>
onTap (2 lines, 1 config point)
<cfparam name="attributes.productid" default="" /> <cfset product = Application.ProductService.getProduct(attributes.productid) />
<cfinclude template="/inc/storeadmin/productdetail.cfm" />
<cfinclude template="/inc/storeadmin/productform.cfm" />
... save the product ... <cfset tap.goto.href = "detail.cfm" />