diff --git a/helpers/helpers.cfm b/helpers/helpers.cfm index 3a555f9..5f683c9 100644 --- a/helpers/helpers.cfm +++ b/helpers/helpers.cfm @@ -36,4 +36,28 @@ function wire(required string name, struct params = {}, string key = "", lazy, boolean lazyIsolated = true ) { return getInstance("CBWIREController@cbwire").wire( argumentCollection=arguments ); } + + /** + * A helper function to set up the view arguments and call the generic wire view. + * Eliminates the need to manually create a basic view that would usually only contains a + * title (optional) and the wire() method call with the appropriate arguments. + * + * @wireName string | The name of the wire component to render. Required. + * @wireParams struct | The parameters to pass to the wire component. Optional, defaults to an empty struct. + * @viewArgs struct | Additional arguments to pass to the view. Optional, defaults to an empty struct. + * @viewArgs.title string | An optional title to display above the wire component in the view. { "title" : "My Cool Page Title" } + * @viewArgs.titleTag string | An optional HTML tag to wrap the title in. Defaults to h1 if title is provided without a titleTag. { "titleTag" : "h2" } + * + * @return void | Sets the events view to the generic wire view with the appropriate arguments. + */ + function wireGenericView( required string wireName, struct wireParams = {}, struct viewArgs={} ) { + // init event if not available in the current context and scope it to this method + if( isNull( event ) ){ + var event = wirebox.getInstance( "coldbox:requestContext" ); + } + // append wire name and params to view args so they can be used in the view + viewArgs._wireName = wireName; + viewArgs._wireParams = wireParams; + event.setView( view = "genericWireView", module="cbwire", args = viewArgs ); + } diff --git a/test-harness/handlers/Main.cfc b/test-harness/handlers/Main.cfc index b9b4e77..3c06855 100644 --- a/test-harness/handlers/Main.cfc +++ b/test-harness/handlers/Main.cfc @@ -8,6 +8,32 @@ component { event.setView( "main/index" ); } + any function testGenericWireView1( event, rc, prc ){ + /* + Bypass the need for a view and just call the wireGenericView() helper directly in the handler method + this will render the specified component in the generic wire view and pass the title and titleTag + arguments to it as well + */ + wireGenericView( + "genericWireViewTesting.genericTest", + {}, + { title = "CBWIRE Test wireGenericView() Helper One", titleTag = "h2" } + ); + } + + any function testGenericWireView2( event, rc, prc ){ + /* + Bypass the need for a view and just call the wireGenericView() helper directly in the handler method + this will render the specified component in the generic wire view and pass the title and titleTag + arguments to it as well + */ + wireGenericView( + "genericWireViewTesting.genericTest", + { "testArgument" : "Test Argument Passed In Succesfully" }, + { title = "CBWIRE Test wireGenericView() Helper Two", titleTag = "h3" } + ); + } + // Run on first init any function onAppInit( event, rc, prc ){ } diff --git a/test-harness/tests/specs/CBWIRESpec.cfc b/test-harness/tests/specs/CBWIRESpec.cfc index 5c5aafe..7ebf066 100644 --- a/test-harness/tests/specs/CBWIRESpec.cfc +++ b/test-harness/tests/specs/CBWIRESpec.cfc @@ -127,6 +127,45 @@ component extends="coldbox.system.testing.BaseTestCase" { expect( beforeHead ).toInclude( "" ); } ); + it( "should use wireGenericView() to set the event view to view=genericWireView & module=cbwire (without wire params)", function() { + + var event = this.get( "Main.testGenericWireView1" ); + var prc = event.getPrivateContext(); + var html = event.getRenderedContent(); + // validate view and module were set properly by wireGenericView() helper + expect( prc.currentView ).toBe( "genericWireView" ); + expect( prc.viewModule ).toBe( "cbwire" ); + expect( prc.currentViewArgs._WIRENAME ).toBe( "genericWireViewTesting.genericTest" ); + // validate custom title tags + expect( html ).toInclude( "
+ No wire component name specified for wireGenericView() method!
+ Please provide a valid component name as the first argument when calling this method.
+