Skip to main content

The Conversion: Lay of the Landing Page - Part 1

In the last post the groundwork has been laid for real development to begin. The data service, router, and landing pages have been created and are ready for code. The last thing to add for this part of the process is the Coldfusion component.

A data folder has been added to store any cfcs relevant to the TRIPS application. The first is to write the CFFUNCTION that will return the list of trips. My development pattern for creating CF functions is fairly routine.  First, decide on a name, parameters and return type.  Sometimes the naming of a function is the hardest part.  I've settled on actionDataObject.  So in this case, the function will be named: getTrips.   

At moment, the landing controller (landing.js) is empty.
 angular.module('core').controller('LandingController',['$scope','dataService', function($scope,dataService){  
 }]);  
The basic task of this controller is getting the list of trips.  First is the establishment of an empty trips array and then the call to get the list of trips via the data service.  Because this call will not need to use any parameters, the generic call can be used.
 angular.module('core').controller('LandingController',['$scope','dataService', function($scope,dataService){  
   $scope.trips = [];  
   dataService.call('getTrips').then(function(data){  
     $scope.trips = data.data;  
   })  
 }]);  
Now to create the getTrips function in CF. The following code snippet is basically the development pattern that I use. Technically, rather than loop through the query results like this function is going to do, you can use serializeJSON(query,'struct') [CF11] which will turn the query into JSON.  But I prefer to roll my own in this case because there may be other processes I want to run or formatting applied that I don't want to have to do in the query. It may be a touch more work for the server and even the developer, but it's clear and easy to maintain.
 <cfcomponent>  
 <cfset _ds = 'myDataSource'>  
 <cffunction name="getTrips" returnformat="json" returntype="array">  
   <cfset _trips = arrayNew(1)>  
   <cfquery name="_qTrips" datasource="#_ds#">  
   </cfquery>  
   <cfloop query="_qTrips">  
     <cfset _trip = structNew()>  
     <cfset arrayAppend(_trips,_trip)>  
   </cfloop>  
   <cfreturn _trips>  
 </cffunction>  
 </cfcomponent>  
The next step is to decide on the fields to be returned. Thinking about the landing page, useful information would be the trip name, trip type, destination, departure date and return date. The query is going to need to look at two tables: trips and trip stops. Also, we want to only return those trips that are relevant to the user.

Fully realized, the function looks something like the following:
 <cffunction name="getTrips" returnformat="json" returntype="array">  
   <cfset _trips = arrayNew(1)>  
   <cfquery name="_qTrips" datasource="#_ds#">  
     select trip_name,  
         author_pidm,  
         destination,  
         trip_type,  
         (select stop_datetime from myschema.tripStops ts where ts.trip_id = t.trip_id and stop_type = 'DEPARTURE_OUTBOUND') departure,  
         (select stop_datetime from myschema.tripStops ts where ts.trip_id = t.trip_id and stop_type = 'ARRIVAL_INBOUND') arrival  
     from  myschema.trips t  
     where  t.author_pidm = <cfqueryparam value="#session.uid#" cfsqltype="cf_sql_integer">  
   </cfquery>  
   <cfloop query="_qTrips">  
     <cfset _trip = structNew()>  
     <cfset _trip.name = trip_name>  
     <cfset _trip.authorID = author_pidm>  
     <cfset _trip.destination = destination>  
     <cfset _trip.type = trip_type>  
     <cfset _trip.outboundDeparture = dateformat(departure,'YYYY-MM-DD')>  
     <cfset _trip.inboundArrival = dateformat(departure,'YYYY-MM-DD')>  
     <cfset arrayAppend(_trips,_trip)>  
   </cfloop>  
   <cfreturn _trips>  
 </cffunction>  
And one more thing with the function.  In CF, the function needs to know it's okay to be called remotely.  To do that, access="remote" is added into the CFFUNCTION tag.

So let's see if it works.

In reviewing the console, it looks like the function is golden.  Now it's time to fill out the landing template.  Part 2 coming up next.

Comments

  1. You were mentioned here: https://dev.lucee.org/t/2017-august-i-love-lucee/2662/2

    I would say that using stored procedures turned my development world 90 degrees because stored procedures can return more than 1 resultset. So I no longer use cfquery believe it or not. I exclusively use the scripted version of calling a stored procedure.

    ReplyDelete
    Replies
    1. Thanks for letting me know about the lucee site. I will definitely check it out. Very interesting about using stored procedures as a replacement to cfquery. Do you find the practice of using stored procs easy to maintain? Do you have production access to deploy them?

      Delete
    2. Well, it's almost like they take the place of components believe it or not because all my stored procedures are in one enormous .sql file. They don't need to be of course, but that's just how it's worked out. Yes, it does add a bit to the workflow because you have to drop/add the stored procedure before you can use it, which is different from simply saving it. But I really like how you can write more than 1 line of sql code at a time (and how a stored procedure can call another stored procedure, which I use _all the time_).

      Delete

Post a Comment

Popular posts from this blog

The Conversion: Introduction

While working as the senior level developer for a small liberal arts college, I've had the luxury of deciding the technology to be used internally for custom applications. When I first started, the college was using Coldfusion 7, HTML, and many MS Access databases. Over the course of the past 11 years, we've moved to Oracle, are on CF11, and utilizing AngularJS / Angular for our front ends.  Coldfusion is still doing the heavy lifting in interfacing with Oracle. During those 11 years, we also found ourselves developing Adobe (now Apache) Flex applications.  Flex was a great tool - easy to develop in and fairly easy to deploy when Flash was ubiquitous. But Flex was not great on mobile platforms.  Hence our move to Angular due to the idea that we wanted to give our users a rich web experience. So over the course of the past two years, we've been either retiring Flex applications altogether due to the direction that some departments have taken (read outside vendor); or r...

The Conversion: Getting Started

So enough about me and my employer from the first two blog posts.  It's time to get some real work started.  This project is about converting the Adobe Flex based TRIPS application to and AngularJS front end.  Now, I have done some work in Angular 2.x (Typescript), but that was a separate deployment into a different (but similar) framework.  Because of the time frame I have to convert this application and it's overall complexity, I am going to stick with AngularJS. Before I get into details, just a quick note about the application framework.  In order to make the management applications easier, I built a small portal based on the concept of users, roles and functions.  All employees can be users of the system provided they accept the FERPA agreement. Access to functionality depends on the roles the user has and which functions are assigned that role. For example, our faculty users have access to class lists and course information via their FACULTY role. ...