Thursday, October 30, 2008

JTemplates template file cached workaround

I've been using a lot of JQuery and JTemplates after reading a couple really helpful JQuery and MVC posts at One problem I ran into while doing some development on a new .Net MVC Project that was using JQuery extensively was that my template files were being cached by my browser. I tried clearing the cache but that wasn't helping, so here's a workaround I came up with.

If you take a look at how JQuery's $.ajax(...) function handles the caching problem you can see that they append a random query string parameter to each request.

So that is what I basically did to workaround the caching problem with the template files. I created a function called cacheBust() that just returned the current times milliseconds value and used that to generate my random query string. Here's some sample code:

function cacheBust() {return new Date().getMilliseconds().toString();}

function GetTimeEntries(day) {
// Show the loady

type: "GET",
cache: false,
url: "/TimeEntry/" + day.replace(/\//g, "-"),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// This method loads the HTML template and
// prepares the container div to accept data.
$('#TimeWrapper').setTemplateURL('/scripts/timeentry.vw?_=' + cacheBust());

// This method applies the JSON array to the
// container's template and renders it.

// Hide the loady.
error: function(XMLHttpRequest, textStatus, errorThrown) {
// typically only one of textStatus or errorThrown
// will have info, I'm not using either.
$('#TimeWrapper').append("<p class='errord'>There was a problem loading the data from the server. Sorry I got nothing else.</p>");



And now we get the most up to the second versions of our templates. Once I'm done with development it will be beneficial to have the templates be cached. I figure once that happens I'll change my cacheBust() function to return a constant (something like, "ItCompilesSoShipIt").

Now Playing: Snow Patrol - Open Your Eyes

Monday, October 6, 2008

Powershell Count Files By Regex Match on Content

Being a Powershell Winter Scripting Games Top Scorer for both the 2007 and 2008 Scripting Games I have to keep my skills fresh. Never know when you need to whip out an extra dirty little script to count how many T's are in a text file.

Scripting languages always give me a dirty feeling when I get done and look at my code. I'm a fan of what Code Complete describes as the Primary Imperative of Software Engineering; Managing Complexity. Solving something with a scripting language always makes we want to write something using the least amount of characters possible. Comments, descriptive variable names and consistent formatting usually go right out the window along with the ability to come back a month later and figure out what is going on in the script, let alone try and tell a co-worker what is going on.

That being said, here is a tiny powershell script to count files based on a Regex match of their content. Don't worry, I took the time to comment it a little bit and even have some pretty descriptive variable names. It originally was just one line for the specific task I needed to get done. I was looking for the number of xml files with a specific value in a set of directories.

# Create Parameters for our script.
# TODO: Make a helpful usage reply when no parameters supplied.
Param( [String]$path="./",
[String]$match=$(throw "Regex Pattern required."),

$reg = [regex]$match;

if ( $searchSubs )
{ # Get the files matching the passed in path/filter
dir -path $path -filter $filter -recurse
# Loop through each and look for a match of the Regex.
% {
$in = gc $_.FullName; $reg.Match($in).Groups[1].Value
} group #Group the Values.
{ dir -path $path -filter $filter
% {
$in = gc $_.FullName; $reg.Match($in).Groups[1].Value
} group

Here is some sample usage:

./FileMatch.ps1 -path c:\somewebsite\fileUpload -filter *.xml -match 'field value="(.*?)" name="blah' -searchSubs

You can even mix up the parameters if you want. Powershell Rocks!

Now Playing: Jason Mraz - Curbside Prophet