Coldbox plugin to help with JavaScript Management Part II
Tags: Coldfusion, Coldbox, Javascript
Today I want to finish of a blog post that I started a little while ago, and give a run down on how it was constructed.
As discussed in my previous blog entry Coldbox plugin to help with JavaScript Management I looked at a way to help minimise the duplication of JavaScript includes, while creating views and layouts in the Application that is running this blog and although it is considered still work in progress, it does highlight how easy it is to create plugins within Coldbox.
As with any plugin that is developed within the Coldbox framework we define an entry point, that is our constructor to initialise the plugin with the details it needs.
<cfargument name="controller" type="any" required="true">
<cfset super.Init(arguments.controller) />
<cfset setpluginName("javascriptManager Plugin")>
<cfset setpluginVersion("1.0")>
<cfset setpluginDescription("Allows for the application to control what javascript files get imported into a layout or view.")>
<cfset instance.javascriptArray = [] />
<cfreturn this>
</cffunction>
The only thing of real interest here is that we are setting an instance variable when the plugin is initialised, and as you can see we are setting this to an Array. Now the assumption here is that this plugin will be running on ColdFusion 8 and hence we are using the new array creation method. So if you need to run this on anything other than ColdFusion 8, you will need to substitute the line above with the following.
In my previous post you would have seen that I used a coldTweet plugin to addJavascript to the manager plugin, which then holds the JavaScript in the array.
<cfargument name="plugin" type="string" required="true" default="" hint="" />
<cfargument name="location" type="string" required="true" default="" hint="" />
<cfargument name="scriptName" type="string" required="true" default="" hint="" />
<cfset var jsStruct = {} />
<cfset jsStruct.plugin = arguments.Plugin />
<cfset jsStruct.location = arguments.Location />
<cfset jsStruct.script = arguments.scriptName />
<cfset ArrayAppend(instance.javascriptArray, jsStruct) />
</cffunction>
As you can see it isn't anything special or spectacular and is virtually just storing the information, and the one thing that is very evident here is that there is no checking to see if what we are storing is not duplicated. The reason behind this at this stage, is that the plugin's are defined as singletons and hence when the are instantiated then we create or store the JavaScript into the array. So the likely hood of it being run a second time is extremely slim, but that doesn't mean it can't get duplicated if a plugin uses a JavaScript that is already being used by another plugin. And that will be my next exercise to enhance this plugin.
So that leaves the function to retrieve the JavaScript out of this plugin.
<cfargument name="plugin" type="string" required="true" default="" hint="" />
<cfargument name="location" type="string" required="true" default="" hint="" />
<cfset var javascriptList = '' />
<cfif refindnocase("(header|footer)", trim(arguments.location))>
<cfloop from="1" to="#ArrayLen(instance.javascriptArray)#" index="arrayCount">
<cfif instance.javascriptArray[arrayCount].plugin eq arguments.plugin and instance.javascriptArray[arrayCount].location eq arguments.location>
<cfset javascriptList = listAppend(javascriptList, instance.javascriptArray[arrayCount].Script) />
</cfif>
</cfloop>
<cfreturn javascriptList />
<cfelse>
<cfthrow type="modules.system.plugins.javascriptManager.InvalidScriptTypeException" message="The script location sent in: #arguments.location# is invalid. Available types: header,footer" />
</cfif>
</cffunction>
As you can see this is a little more complicated than the previous function, and the only reason behind that as you can see we throw an error if the check for what location we are trying to retrieve doesn't match the requirement.
Now that is pretty much it for the javascriptManager for Coldbox, feel free to use this and if you modify or enhance the script along the way I would be very interested to include the enhancements into the repository that will be going up onto riaforge soon.
There are no comments for this entry.



TweetBacks