Skip to main content

CF: Scripting amidst the tags

Recently I had to do a quick "utility" page where I needed to see how many files from a directory listing had been recorded into a database. I've been writing about 98% of my CF code in script syntax but, since it was quick and easy, I did this quickly in tags because it was outputting directly to the browser. In doing so, I made an interesting discovery in that, when you use closures, even in tag based pages, you can write cfscript. Here's the example

  1. Get the directory listing: 

    <cfset alljs = directoryList(expandpath('/src'),true,"path","*.js")>

  2. Get the database listings and convert it to an array

    <cfquery name="alljsQ" datasource="blah">
    select * from sitefiles where filename like '%.js%'
    </cfquery>
    <cfset recordedFiles = valuelist(alljsQ.filename).listToArray()>

  3. Use a filter function to weed out the files I'd already recorded
    <cfset missingFiles = alljs.filter(function(item){
        var foundFile=item.replacenocase(expandpath('/src\'),'','all').replace("\","/","all");
    return recordedFiles.find(foundFile) eq 0; })>
  4. Use a map function to do what I needed to do with the files not recorded

    <cfset missingFiles.map(function(item){
    var foundFile=item.replacenocase(expandpath('/src\'),'','all').replace("\","/","all");
    writeOutput("<li>#foundFile#</li>");
    })>
A couple of notes about this:
  1. 1. Using the filter and map functions make the code more "functional". If you're looking for a reason to experiment with the idea of functional code, here are two good reasons:
    1.  It really diminishes the number of things you can screw up. 
    2. You can do it, even using tag based code which might ease the process of moving into script

  2. In the filter function in step 3, I used the member function ".find()". This is the equivalent of ArrayFind(recordedFiles,foundFile) but is more readable. Incidentally, I actually used ".indexOf" but then was a bit thrown when I had to use "eq -1" until I remembered that the CF version is ".find" and therefore starts counting at 1 with 0 being "not found". The Java version is ".indexOf" which means it starts counting at 0 and therefore -1 is the "not found".

  3. In the map function, the "callback" or "closure" function that is passed in, can be written in cfscript without worrying about the <cfscript> tag and so on. 

Perhaps obvious to everyone but I did this mostly without thinking and then was pleasantly surprised when it worked. 


Comments

Popular posts from this blog

Creating Stories and Tasks in Jira: Personas and our Software Development Team

Part of the CI/CD Development Series The next step is developing who is on our hypothetical development team. Given that it has a React front end and ColdFusion as the Server Side language, I came up with the following personas, all of which have their own needs and considerations for our development environment. I've listed all the jobs that need doing, not the people involved since, even on a small team or a team of one, these "hats" are all worn by someone, even if it's the same person. Personas for our Project Dev Ops Coordinator - The person responsible for smooth and accurate deployments CF Developer - The person responsible for the API and fulfillment code development and maintenance. React Developer - The person responsible for the front end development Database Coordinator - The person responsible for the schema, data, up time and, presumably the testing databases used by the developers. Lead Developer - The person responsible for coordinat...

More on Site Architecture / CI/CD / and Repos

We're starting to move from the high level overview and more into the details of our project.  Architecture We established that we using React as our front end technology and ColdFusion as our server side. At a more granular level, we'll be using React with Redux for our front end app and we want our server side to be not just "ajax" enabled but a true REST API. To this end, we're going to incorporate Coldbox from Ortus Solutions. Repos These two code bases have different needs, and possibly locations, when they are deployed. As a result, we're going to have two repositories: one for the React and another for the API. This will allow us to keep our code separated and cleaner all the way through to deployment.  For each of these repos, we'll follow the procedure we laid out previously with the MASTER branch being used to deploy to production and a DEVELOPMENT branch being used for active work which is then fed into the Master branch via pull requests.  Test...