Skip to content

Commit

Permalink
Initial import of files into repository
Browse files Browse the repository at this point in the history
  • Loading branch information
cutterbl committed Feb 20, 2012
1 parent 9145ffd commit c20be04
Show file tree
Hide file tree
Showing 10 changed files with 8,660 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Application.cfc
@@ -0,0 +1,15 @@
<cfcomponent displayname="Application" output="false" >

<cfscript>
THIS.name = "CFQueryToForm";
THIS.applicationTimeout = createTimespan(0,2,0,0);
THIS.dataSource = "cftftest"; // With this I don't need to include the attribute in my cfquery tags
THIS.serverSideFormValidation = false; // this is 'true' by default, but I like to write my own
THIS.sessionManagement = true;
THIS.sessionTimeout = createTimespan(0,0,20,0);
THIS.setClientCookies = true;
THIS.setDomainCookies = true;
THIS.timeout = 3000; // This overrides the CF Admin's request timeout value, in milliseconds
</cfscript>

</cfcomponent>
Empty file removed README
Empty file.
13 changes: 13 additions & 0 deletions README.txt
@@ -0,0 +1,13 @@
The 'cftf' folder, to which this README file is in, is considered the root folder of the demo site. Should you change the root, make sure to change any relevant pathing to match.

This encompasses two separate JQuery Plugins: CFRequestToForm, and CFQueryToForm

CFRequestToForm is reliant upon CFQueryToForm, but not vice versa.

CFQueryToForm will take ColdFusion's native JSON serialization of it's native ColdFusion Query object, and apply the result to a form's fields.

CFRequestToForm will take a configuration object that will then make an Ajax request, based upon the configuration, and automatically apply the result to the form is was applied upon.

See the js file for documentation of usage.

A MySQL script can be found in the /resources/db/ folder that will create the sample table used by the demo. This example should be compatible with ColdFusion 8 or higher.
38 changes: 38 additions & 0 deletions com/cc/Users/Users.cfc
@@ -0,0 +1,38 @@
<cfcomponent displayname="Users" output="false">

<cffunction name="getUser" displayname="getUser" hint="Gets a User by userId" access="remote" output="false" returntype="struct">
<cfargument name="userId" type="numeric" hint="The userId of the user to return" required="false" default="0" />
<cfset var LOCAL = StructNew() />
<cfset LOCAL.retVal = StructNew() />
<cfset LOCAL.retVal['success'] = true />
<cftry>
<cfquery name="LOCAL.user">
SELECT userId,
email,
firstName,
lastName,
sex,
isActive,
state,
info
FROM Users
<cfif ARGUMENTS.userId>
WHERE userId = <cfqueryparam cfsqltype="cf_sql_integer" value="#ARGUMENTS.userId#" />
<cfelse>
ORDER BY firstName
</cfif>
</cfquery>
<cfif LOCAL.user.recordCount>
<cfset LOCAL.retVal['result'] = LOCAL.user />
<cfelse>
<cfthrow type="My_Custom" errorcode="0001" message="No User was found matching your request." />
</cfif>
<cfcatch type="any">
<cfset LOCAL.retVal['success'] = false />
<cfset LOCAL.retVal['message'] = CFCATCH.message />
</cfcatch>
</cftry>
<cfreturn LOCAL.retVal />
</cffunction>

</cfcomponent>
52 changes: 52 additions & 0 deletions index.cfm
@@ -0,0 +1,52 @@
<cfsetting enablecfoutputonly="true" />

<cfset REQUEST.allUsers = CreateObject("component","com.cc.Users.Users").getUser() />
<!---<cfdump var="#REQUEST.allUsers#" abort="true" />--->
<cfoutput>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="/resources/scripts/jquery/jquery-1.5.1.js"></script>
<script type="text/javascript" src="/resources/scripts/jquery/cfquerytoform.jquery-0.1.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('select##userId').change(function(e){
$('form[name="user"]').CFRequestToForm('/com/cc/Users/Users.cfc',"getUser",{userId: $(this).val()},"result");
});
});
</script>
</head>
<body>
<div id="pgContent">
<select id="userId">
<option value=""></option>
<cfloop query="REQUEST.allUsers.result">
<option value="#REQUEST.allUsers.result.userId#">#REQUEST.allUsers.result.firstName#</option>
</cfloop>
</select>
<hr />
<form name="user">
<input type="hidden" name="userId" value="" />
<label for="firstName">First Name:</label> <input type="text" name="firstName" value="" /><br />
<label for="lastName">Last Name:</label> <input type="text" name="lastName" value="" /><br />
<label for="email">Email:</label> <input type="text" name="email" value="" /><br />
<label for="sex">Sex:</label> <input type="radio" name="sex" value="M" /> Male <input type="radio" name="sex" value="F" /> Female<br />
<label for="state">State:</label>
<select name="state">
<option value=""></option>
<cfloop query="REQUEST.allUsers.result">
<option value="#REQUEST.allUsers.result.state#">#REQUEST.allUsers.result.state#</option>
</cfloop>
</select>
<input type="checkbox" name="isActive" value="1" /> <label for="isActive">Active</label><br />
<label for="info">Info:</label><br />
<textarea name="info" cols="80" rows="10"></textarea>
</form>
</div>
</body>
</html>
</cfoutput>

<cfsetting enablecfoutputonly="false" />
62 changes: 62 additions & 0 deletions resources/db/cftf-demo.sql
@@ -0,0 +1,62 @@
-- MySQL Administrator dump 1.4
--
-- ------------------------------------------------------
-- Server version 5.1.52-community


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;


--
-- Create schema cftftest
--

CREATE DATABASE IF NOT EXISTS cftftest;
USE cftftest;

--
-- Definition of table `users`
--

DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`userId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(65) NOT NULL,
`firstName` varchar(45) NOT NULL,
`lastName` varchar(45) NOT NULL,
`sex` char(1) CHARACTER SET ucs2 NOT NULL,
`isActive` tinyint(3) unsigned NOT NULL,
`state` char(2) NOT NULL,
`info` varchar(255) NOT NULL,
PRIMARY KEY (`userId`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

--
-- Dumping data for table `users`
--

/*!40000 ALTER TABLE `users` DISABLE KEYS */;
INSERT INTO `users` (`userId`,`email`,`firstName`,`lastName`,`sex`,`isActive`,`state`,`info`) VALUES
(1,'web.admin@cutterscrossing.com','Cutter','Blades','M',1,'VA','Testing info record 1: Cutter'),
(2,'number.one@cutterscrossing.com','Teresa','Blades','F',1,'MD','Testing info record 2: Teresa'),
(3,'number.two@cutterscrossing.com','Savannah','Blades','F',1,'FL','Testing info record 3: Savannah'),
(4,'the.dog@cutterscrossing.com','Adama','Blades','M',0,'GA','Testing info record 4: Adama');
/*!40000 ALTER TABLE `users` ENABLE KEYS */;




/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
138 changes: 138 additions & 0 deletions resources/scripts/jquery/cfquerytoform.jquery-0.1.js
@@ -0,0 +1,138 @@
/**
* TEMPLATE
* CFQueryToForm.jquery-0.1.js
*
* VERSION
* 0.1 (03.15.2011)
*
* AUTHOR
* Steve 'Cutter' Blades, webDOTadminATcutterscrossing.com
*
* LICENSE
* This document is licensed as free software under the terms of the
* MIT License: http://www.opensource.org/licenses/mit-license.php
*/

/**
* FUNCTION
* CFQueryToForm
*
* PURPOSE
* This function is used in conjunction with data automatically converted to JSON from ColdFusion's native
* query object. It takes two arguments: 'data', a JSON object, and 'root' the key containing the CF query
* object. It will then loop the values in the query, and apply them to the coinciding form fields. This requires
* that the column names, of the query, are a match to the field names in the form. The 'ucColNames' argument is an
* optional boolean argument, defaulted to true. This argument tells our CFQueryToForm plugin to uppercase fieldnames
* prior to comparing to column names. Passing 'false' to this argument will tell the parser to use exact case
* matching instead. This is important for some frameworks. This is used to process query requests that return
* a single record query object.
*
* BASIC USAGE
* $('#someForm').CFQueryToForm(retData, 'data');
*
* This code makes some assumptions, requiring some structure to the data returned. The method
* called from the CFC must return a structure:
* {
* 'success': boolean denoting the success/failure of the server side process,
* [root]: the query return with the record to populate the form,
* ['message']: if success is 'false', then this is how the server-side process failed
* }
* @param {Object} data
* @param {String} root
* @param {Boolean} ucColNames
*/

(function( $ ){
$.fn.CFQueryToForm=function(data, root, ucColNames) {
var form = $(this),
success = true,
ucColNames = ucColNames||true,
fields, select;
fields = form.children('input').toArray();
fields = fields.concat(form.children('select').toArray());
fields = fields.concat(form.children('textarea').toArray());
$.each(fields, function(i, v){
var fld = $(v),
nm = fld.attr('name'),
tag = v.tagName,
pos = $.inArray(((ucColNames) ? nm.toUpperCase() : nm), data[root].COLUMNS),
type = fld.attr('type')||'select';
if(pos > -1){
switch(tag){
case "INPUT":
if(type === 'radio'||type === 'checkbox'){
(fld.attr("value").toString() === data[root].DATA[0][pos].toString())?fld.attr('checked','checked'):fld.removeAttr('checked');
break;
}
default:
$(v).val(data[root].DATA[0][pos]);
break;
}
}
});
return success;
};
})( jQuery );

/**
* FUNCTION
* CFRequestToForm
*
* PURPOSE
* This function is used in conjunction with the ColdFusion server, making a remote request
* of a ColdFusion Component (CFC) for query data, and populating a form with the return.
*
* BASIC USAGE
* $('#someForm').CFRequestToForm('/path/to/my.cfc','myRemoteMethod',{someId:42},'result');
*
* This code makes some assumptions, requiring some structure to the data returned. The method
* called from the CFC must return a structure:
* {
* 'success': boolean denoting the success/failure of the server side process,
* [root]: the query return with the record to populate the form,
* ['message']: if success is 'false', then this is how the server-side process failed
* }
*
* This plugin will make a request to the configured 'url', passing any parameters through the 'postData' configuration
* object. It will automatically pass ColdFusion attributes to convert the return (struct) into a JSON object. For further
* understanding, please see the following blog post on dealing with Ajax requests. The returned ColdFusion query data will
* be applied to the form the plugin was configured to. The 'ucColNames' argument is an optional boolean argument, defaulted to true.
* This argument tells our CFQueryToForm plugin to uppercase fieldnames prior to comparing to column names. Passing 'false' to
* this argument will tell the parser to use exact case matching instead. This is important for some frameworks.
*
* http://www.cutterscrossing.com/index.cfm/2011/9/26/How-I-Do-Things-ColdFusion-and-Ajax-Requests
*
* A 'failed' request will throw a 'cffail' event. The developer can bind to this event, expecting the same basic
* arguments that a standard JQuery ajax 'error' would receive (jqXHR, textStatus, errorThrown). The 'errorThrown' will be the complete 'data'
* object returned from the server.
*
* @param {String} url
* @param {String} methodName
* @param {Object} postData
* @param {String} root
* @param {Boolean} ucColNames
*/
(function( $ ){
$.fn.CFRequestToForm=function(url, methodName, postData, root, ucColNames) {
var form = $(this),
postData = postData||{},
ucColNames = ucColNames||true,
success = true;
console.log(ucColNames);
$.ajax({
url: url,
data: $.extend(true, {}, postData, {method: methodName, returnFormat: "JSON"}),
dataType: "json",
success: function(d, r, o){
if(d.success){
form.CFQueryToForm(d, root, ucColNames)
} else {
var e = $.Event("cffail");
form.trigger(e, [o, r, d]);
success = false;
}
}
})
return success;
};
})( jQuery );

0 comments on commit c20be04

Please sign in to comment.