Sam Farmer head shot

Sam Farmer

Growing up I never imagined I would play bass guitar for the Dave Matthews Band. And indeed it never happened.

But I have become a passionate and pretty good web developer.


Slides for jQuery & Media Queries: Optimize your screen or app for every screen size

Here are my slides for the presentation I gave at RIACON on jQuery & Media Queries: Optimize your screen or app for every screen size. I really enjoyed giving this presentation and got some great questions in the session. And if you are looking for a good example of Media Queries take a look at Checklists and make the browser window smaller and bigger.

RIACON is less than two weeks away; Register NOW!

With 25 sessions spread over three tracks — ColdFusion, Flex and jQuery/Javascript — RIACON is shaping up to be a great conference.

This is the inaugural conference and the registration cost is $99. How can you not want to come? Register now

I will be talking on jQuery and Media Queries: Optimize your site or app for every screen size

Simple Example of jQuery Templates Loading JSON data

jQuery templates are a cool way to tie a datasource (often json, but at least arrays of maps/structurscriptput. First we define a template which are placed within script tags so that the browser ignores the html inside it:

view plain print about
1<script id="artistsTemplate" type="text/x-jquery-tmpl">
2    <div><h4>#${artistid} ${firstname} ${lastname}</h4>${city} ${state}</div>
3</script>

jQuery templates will loop over the array of maps/structures and for each ${xxx} replace the it with the value of a key of the same name (xxx). Quick sample of the first two records of data:

view plain print about
1[
2 {"state":"CO","lastname":"Donolan","firstname":"Aiden","artistid":1,"city":"Denver"},
3 {"state":"CA","lastname":"Weber","firstname":"Austin","artistid":2,"city":"Berkeley"}
4]

The output from that we will place in the following div:

view plain print about
1<div id="artistsContent"></div>

Data will be loaded each time we click on the Load Data button:

view plain print about
1<div><button iscript">Load Data</button> <span id="lastLoad"></span></div>

Here is the jQuery that makes it all happen:

view plain print about
1<script type="text/javascript">
2$(document).ready(function(){
3$("#getData").click( function() {
4    $.getJSON("artistsRemote.cfc?method=getArtists&returnformat=json",
5        function(data){
6            //empty out the div that will hold the generated content
7            $("#artistsContent").empty();
8            //call the tmpl function, pass in the data and have it append to artistsContainer
9            $("#artistsTemplate").tmpl( data ).appendTo("#artistsContent");
10            //indicate when last loaded
11            var nowIs = new Date().toLocaleString();
12            $('#lastLoad').html( nowIs );
13        });
14});
15});
16</script>

And what it looks like:

Getting the json data from ColdFusion is pretty easy. Here is the full ArtistsRemote.cfc (pick one of the return statements!):

view plain print about
1component {
2remote function getArtists() {
3    return entityLoad( "Artists", {}, "artistid");
4    return ormExecuteQuery(" SELECT new map(artistid as artistid, firstname as firstname, lastname as lastname,
5        city as city, state as state ) FROM Artists ORDER BY artistid");
6}
7}

The first return statement will serialize the whole object, the second just the five properties we will display.

Application.cfc:

view plain print about
1component {
2    this.name = "templatesSample";
3    this.datasource = "cfartgallery";
4    this.ormEnabled = true;
5}

For this example I am letting ColdFusion introspect the database for all columns which makes Artists.cfc pretty small:

view plain print about
1component persistent="true" {
2}

Finally for reference here is the full html page:

view plain print about
1<html>
2<head>
3    <title>Template Fun</title>
4    <style type="text/css">
5        body { font-family: Arial; font-size: 0.8em; }
6        h4 { margin: 0.5em 0 0 0 }
7    </style>
8    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
9    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script>
10<script type="text/javascript">
11$(document).ready(function(){
12$("#getData").click( function() {
13    $.getJSON("artistsRemote.cfc?method=getArtists&returnformat=json",
14        function(data){
15            //empty out the div that will hold the generated content
16            $("#artistsContent").empty();
17            //call the tmpl function, pass in the data and have it append to artistsContainer
18            $("#artistsTemplate").tmpl( data ).appendTo("#artistsContent");
19            //indicate when last loaded
20            var nowIs = new Date().toLocaleString();
21            $('#lastLoad').html( nowIs );
22        });
23});
24});
25</script>
26</head>
27<body>
28
29<div><button id="getData">Load Data</button> <span id="lastLoad"></span></div>
30
31<script id="artistsTemplate" type="text/x-jquery-tmpl">
32    <div><h4>#${artistid} ${firstname} ${lastname}</h4>${city} ${state}</div>
33</script>
34
35<div id="artistsContent"></div>
36
37</body>
38</html>

BlogCFC was created by Raymond Camden. This blog is running version 5.9.7. Contact Blog Owner