Its been awhile since I've done a ColdFusion quicke post and I found this trick to be quite useful especially when dealing with ORM domain entities.
Lets say that we have a domain object called User that gets populated out when a user signs up for an account. This object has some simple properties; fullName and email (lets not get into object purity here and say that those should be a member of a Contact object). Lets also assume that when a user signs up all only an e-mail address is required, they can leave their name blank if they choose to. The important thing to note here is that when a user object is loaded from the database the fullName property may hold a value of null.
When a user signs up for an account our application calls a function that will send a welcome message to a user that has a signature of: sendWelcomeMessage(required String name, required String email) (for arguments sake lets say that we don't have the ability to change. If this function is invoked using implied getters from the User object we'll run into problems if getFullName() returns null. This is where the ternary operator can quite useful in sending a default value to a function:
sendWelcomeMessage(
(!isNull(person.getFullName()) ? user.getFullName() : "User" ,
user.getEmail
);
The ternary operator takes a value or expression that can be evaluated to a Boolean and returns one of two things based on the result of that expression. The ternary operator's signature looks like this:
(expression) ? trueStatement : falseStatement
As you can see in our example, we check if person.getFullName()does not return null and pass that value if its true, otherwise pass in a default value of "User".
Thursday, September 6, 2012
Tuesday, September 4, 2012
jQuery Function Of The Week: Deferred (deep dive)
This week we'll take a deeper look into jQuery's Deferred object. According to the jQuery docs:
If you've used jQuery's AJAX functions you've already been taking advantage of Deferreds, the AJAX functions are actually using Deferrds behind the scenes with a lot of the details abstracted out for you.
When talking about Deferreds its important to note that there are two parts to each Deferred implementation: the callers and the callees. The callers are the functions that either do something that other functions are waiting for or functions that take action based on the results of another function. The callees are the functions that depend on the results of at least one of the callee functions.
The Callers
Like I mentoned before, the caller functions are divided into two parts: the functions that do something that other functions depend on, and the functions that take action based on the the results of other functions. The when(deferrds)function is the first part of the callers and is a wrapper for all of the functions have results that other functions are waiting for. jQuery will execute the functions passed into when immediately and asynchrouously. This means if we have the following code:
func1(), func2(), and func()will all be called at approximately the same time.
Functions passed into when are expected to return a promise object back to the when() function. This promise will either be resolved or rejected (more on these in the callees section) by the function passed into when, and based on this result one of callback functions will be called.
The second part of the callers are the functions that respond to the results of the functions passed into the when function. The three functions are: then, done, and fail. The then(doneCallbacks, failCallbacks) is a single method that encapsulates the functionality of the done() and fail() callbacks. The first argument to the then function is a function, or array of functions, that will be called when all the functions passed into the when() function are resolved.
The second argument passed into the then function is a function, or array of functions, that will be immediately invoked as soon as any of the functions passed into the when() function are rejected. The then and done functions will receive arguments from each function passed into the when function, in the order they were passed. These arguments are passed into them via the resolve and reject methods (we will talk about when we discuss the callees). If you'd like to be a bit more expressive in your code instead of using the then() you can specify the doneCallbacks and failCallbacks in the done() and fail() methods. These methods are called the same was as the doneCallbacks, and failCallbacks of the then() function.
The Callees
When functions are passed into the when function jQuery expects these functions to return a promise to it. A promise is just a message telling the when function that sometime in the future the function is either going to send it a reject( args ) or resolve( args ) message. Once the when function receives the promise object from a function it just sits and waits for a response. The callee functions can also pass information back to the when function by passing arguments into the reject and resolve methods (I'll show you an example of this in the real world example). Lets take a look at a typical callee function that implements the Deferred object and talk a bit more about what is going on:
Here we have a function that is going to implement the $.Deferred() function and immediately return a promise message to the when() function that called it. The $.Deferred() constructor function takes an optional function as an argument which is passed a reference to itself as an argument. The above function could also have been rewritten as:
Its important to note that if a function passed into when does not implement this Deferred pattern it will immediately be treated as resolved.
Real World Example
Now that we've looked at the parts of the Deferred lets take a look at a real world example. In order to demonstrate the power of Deferred's lets take a look at some code that can benefit from them:
JS Bin
The intent of this code is to fade out any current content, get some tweets from twitters search API, and finally display the new tweets. When any of the buttons are pressed you'll quickly see that something isn't right, and this is because the code being called relies on asynchronous operations like doing an AJAX call and jQuery animations.
One approach to fix this would be to put the getTweets function as a callback to the cleanDiv function's fadeOut callback, move the addTweets function to the ajax calls success callback, and finally move the the $tweetDiv.fadeIn('slow') code to the addTweets function. Doing so would tightly couple this code together making it tough to extend and maintain. A better solution would to use the power of jQuery's Deferred object.
First lets look at our click handler function rewritten to take advantage of the Deferred object:
The first thing you probably notice is that there is more code in the new click handler. The Deferred calls do add a bit of code, but I feel this code is quite expressive (meaning that the intent of the code is very clear) and each of the callback functions are decoupled from any function they are waiting on. One other thing to note here is the use of the done() and fail() callbacks. In the done() callback I use the when() and then() methods of the Deferred implementation. Here I am only specifying a single argument which will get called if/when the addTweets function passed into it has completed. Since I'm not going to reject the promise anywhere in the addTweets function only passing in a done callback is sufficient.
Now lets take a look at one of the new callee functions. Here is the code for the new getTweets function:
Since there is no access to the internal representation of the $.ajax method's Deferred function I have to wrap the entire function in a Deferred call. Another thing to note is that Twitter's API will return a 200 status (which will invoke the ajax function's success callback) even if there is an error so I will inspect the object returned from Twitter when determining weather to resolve or reject the Deferred object. If a key of "error" is found in the data returned from Twitter I reject the Deferred which tells our click handler function to run the fail callback. I'll also pass a message to the fail callback through the reject method call.
Putting it all together we get a nice and smooth implementation of our application. As you can see I've also included an error button which will trigger an error in the twitter API.
JS Bin
jQuery.Deferred()
, introduced in version 1.5, is a chainable utility object that can register multiple callbacks into callback queues, invoke callback queues, and relay the success or failure state of any synchronous or asynchronous function.
That is a bit of a mouthful. I like to think of deferrds somewhat like a telephone call. You'd like to contact someone so you dial a number. Based on what happens when you call that number you will take action. If the callee answers the call you can start a conversation, if you get their voice mail (I almost said answering machine but I'm afraid that some readers will have no idea what that is) you can take leave a message. Also, when the phone is ringing you can even do something else, you only care about what happens once the number you are calling responds in some way. This is an extremely simplified way of describing how Deferreds work because Deferrds can do so much more. If you've used jQuery's AJAX functions you've already been taking advantage of Deferreds, the AJAX functions are actually using Deferrds behind the scenes with a lot of the details abstracted out for you.
When talking about Deferreds its important to note that there are two parts to each Deferred implementation: the callers and the callees. The callers are the functions that either do something that other functions are waiting for or functions that take action based on the results of another function. The callees are the functions that depend on the results of at least one of the callee functions.
The Callers
Like I mentoned before, the caller functions are divided into two parts: the functions that do something that other functions depend on, and the functions that take action based on the the results of other functions. The when(deferrds)function is the first part of the callers and is a wrapper for all of the functions have results that other functions are waiting for. jQuery will execute the functions passed into when immediately and asynchrouously. This means if we have the following code:
func1(), func2(), and func()will all be called at approximately the same time.
Functions passed into when are expected to return a promise object back to the when() function. This promise will either be resolved or rejected (more on these in the callees section) by the function passed into when, and based on this result one of callback functions will be called.
The second part of the callers are the functions that respond to the results of the functions passed into the when function. The three functions are: then, done, and fail. The then(doneCallbacks, failCallbacks) is a single method that encapsulates the functionality of the done() and fail() callbacks. The first argument to the then function is a function, or array of functions, that will be called when all the functions passed into the when() function are resolved.
The second argument passed into the then function is a function, or array of functions, that will be immediately invoked as soon as any of the functions passed into the when() function are rejected. The then and done functions will receive arguments from each function passed into the when function, in the order they were passed. These arguments are passed into them via the resolve and reject methods (we will talk about when we discuss the callees). If you'd like to be a bit more expressive in your code instead of using the then() you can specify the doneCallbacks and failCallbacks in the done() and fail() methods. These methods are called the same was as the doneCallbacks, and failCallbacks of the then() function.
The Callees
When functions are passed into the when function jQuery expects these functions to return a promise to it. A promise is just a message telling the when function that sometime in the future the function is either going to send it a reject( args ) or resolve( args ) message. Once the when function receives the promise object from a function it just sits and waits for a response. The callee functions can also pass information back to the when function by passing arguments into the reject and resolve methods (I'll show you an example of this in the real world example). Lets take a look at a typical callee function that implements the Deferred object and talk a bit more about what is going on:
Here we have a function that is going to implement the $.Deferred() function and immediately return a promise message to the when() function that called it. The $.Deferred() constructor function takes an optional function as an argument which is passed a reference to itself as an argument. The above function could also have been rewritten as:
Its important to note that if a function passed into when does not implement this Deferred pattern it will immediately be treated as resolved.
Real World Example
Now that we've looked at the parts of the Deferred lets take a look at a real world example. In order to demonstrate the power of Deferred's lets take a look at some code that can benefit from them:
JS Bin
The intent of this code is to fade out any current content, get some tweets from twitters search API, and finally display the new tweets. When any of the buttons are pressed you'll quickly see that something isn't right, and this is because the code being called relies on asynchronous operations like doing an AJAX call and jQuery animations.
One approach to fix this would be to put the getTweets function as a callback to the cleanDiv function's fadeOut callback, move the addTweets function to the ajax calls success callback, and finally move the the $tweetDiv.fadeIn('slow') code to the addTweets function. Doing so would tightly couple this code together making it tough to extend and maintain. A better solution would to use the power of jQuery's Deferred object.
First lets look at our click handler function rewritten to take advantage of the Deferred object:
The first thing you probably notice is that there is more code in the new click handler. The Deferred calls do add a bit of code, but I feel this code is quite expressive (meaning that the intent of the code is very clear) and each of the callback functions are decoupled from any function they are waiting on. One other thing to note here is the use of the done() and fail() callbacks. In the done() callback I use the when() and then() methods of the Deferred implementation. Here I am only specifying a single argument which will get called if/when the addTweets function passed into it has completed. Since I'm not going to reject the promise anywhere in the addTweets function only passing in a done callback is sufficient.
Now lets take a look at one of the new callee functions. Here is the code for the new getTweets function:
Since there is no access to the internal representation of the $.ajax method's Deferred function I have to wrap the entire function in a Deferred call. Another thing to note is that Twitter's API will return a 200 status (which will invoke the ajax function's success callback) even if there is an error so I will inspect the object returned from Twitter when determining weather to resolve or reject the Deferred object. If a key of "error" is found in the data returned from Twitter I reject the Deferred which tells our click handler function to run the fail callback. I'll also pass a message to the fail callback through the reject method call.
Putting it all together we get a nice and smooth implementation of our application. As you can see I've also included an error button which will trigger an error in the twitter API.
JS Bin
Labels:
JavaScript,
jQuery
Tuesday, August 14, 2012
jQuery Function of the Week: grep
This week's jQuery function of the week is grep. According to the jQuery docs grep:
Lets take a look at looping through an array of text and removing the word "the":
JS Bin demo
Now lets take a look at the same code, but with the invert parameter set to true:
JS Bin demo
You'll see that only the word "the" is returned.
Check out the all of the jQuery function's of the week.
Finds the elements of an array which satisfy a filter function. The original array is not affected.There is only a single signature for the grep function: $.grep(array, function(element, index) [,invert]). The function being passed in should return a truthy value or a falsy value based on the input argument. If the invert parameter is left out, or is false then the grep function will return a new array with all the items that the satisfy the truth test function that was passed in. If invert is set to true, grep will return a new array with all the items that do not satisfy the truth test function.
Lets take a look at looping through an array of text and removing the word "the":
JS Bin demo
Now lets take a look at the same code, but with the invert parameter set to true:
JS Bin demo
You'll see that only the word "the" is returned.
Check out the all of the jQuery function's of the week.
Labels:
JavaScript,
jQuery
Thursday, August 9, 2012
jQuery Function Of The Week: nextAll
I'm super busy this week so this week's jQuery function of the week is going to be an easy one: nextAll. According to the jQuery Documentation use nextAll to:
JS Bin demo
Optionally a selector can be passed into nextAll to further filter the results. Here we'll only select the even vegetables that are after our original selector:
JS Bin demo
Get all following siblings of each element in the set of matched elements, optionally filtered by a selector.Lets say we had a list of items, and we wanted to highlight every vegetable Using nextAll would look like this:
JS Bin demo
Optionally a selector can be passed into nextAll to further filter the results. Here we'll only select the even vegetables that are after our original selector:
JS Bin demo
Labels:
JavaScript,
jQuery
Wednesday, August 1, 2012
jQuery Function Of The Week: proxy
In my JavaScript fundamentals presentations I have a section that talks about scope and how it can become confusing when using jQuery. The reason for this is becuase jQuery normalizes the this scope in its function calls. In other words when inside a jQuery method call this usually refers to the DOM elements in the jQuery set.
This can be clearly illustraed in a simple jQuery event callback (note use your browsers console, not jsbin's for the meantime):
JS Bin demo
This can become a problem when we try to write reusable code. Lets say we want to put our event handler into a object and then call it when a user clicks on a link:
JS Bin Demo
If you run the code you'll see that you get "Hello, undefined" instead of "Hello, Homer Simpson". The reason is simple, jQuery used the link as the context of this where no myName property exists. Enter jQuery.proxy.
According to the jQuery Documentation, the proxy function:
JS Bin Demo
The change is subtle, but the result is exactly what we want. Now, at this point you might be asking yourself, "isn't this the same thing as using the apply() or call() methods (mdn apply documentation)? The answer is yes...and no. Lets look at how to get the desired result using apply():
JS Bin Demo
If you look closely you'll notice a slight difference, the callback is now invoked in an anonymous function instead of passed directly to the on() method. This is because the apply method invokes the function immediately whereas jQuery's proxy function only returns a reference function for later invocation. This is a bit nicer because it makes for slightly more terse code.
In addition the proxy() function does a few more things:
This can be clearly illustraed in a simple jQuery event callback (note use your browsers console, not jsbin's for the meantime):
JS Bin demo
This can become a problem when we try to write reusable code. Lets say we want to put our event handler into a object and then call it when a user clicks on a link:
JS Bin Demo
If you run the code you'll see that you get "Hello, undefined" instead of "Hello, Homer Simpson". The reason is simple, jQuery used the link as the context of this where no myName property exists. Enter jQuery.proxy.
According to the jQuery Documentation, the proxy function:
Takes a function and returns a new one that will always have a particular context.This means that by using the $.proxy function we can make our callback run in the proper context, in this case itself:
JS Bin Demo
The change is subtle, but the result is exactly what we want. Now, at this point you might be asking yourself, "isn't this the same thing as using the apply() or call() methods (mdn apply documentation)? The answer is yes...and no. Lets look at how to get the desired result using apply():
JS Bin Demo
If you look closely you'll notice a slight difference, the callback is now invoked in an anonymous function instead of passed directly to the on() method. This is because the apply method invokes the function immediately whereas jQuery's proxy function only returns a reference function for later invocation. This is a bit nicer because it makes for slightly more terse code.
In addition the proxy() function does a few more things:
- it makes sure that the function passed in is actually a function
- it assignes a unique ID to the function. This is very important because when trying to unbind a function you may not be unbinding the proper function if you are passing in the function to the off() method. Its best to always use namespaced events when using on() and off() with $.proxy().
Labels:
JavaScript,
jQuery
Subscribe to:
Posts (Atom)