Expressions in a JavaScript Switch Case

Today I stumbled across something I'd never seen before, and at first thought had to be an error - a switch statement with expressions embedded in the case statement:

What the heck is that thing?

 


jsFiddle: Cleaning Copy/Paste Input for ContentEditable

This is captured in an AngularJS directive, but the process could easily be bound with jQuery instead and follow the same steps. The basics here are to stop the default paste, read the paste data as plain text, and then to replace some risky characters (necessary for my exact use case).

As noted, this example has been built for and tested in Chrome only - I know for IE, you need to potentially handle a different getData() operation.

 

jsFiddle : Angularized Gauge Display

I kicked up my last example by converting the gauge into an Angular directive.

 

jsFiddle : Circular Gauge with Raphael

A single 0-to-100 percent circular gauge done with Raphael. It is largely based off of a StackOverflow posting, but I added the text display and color animations as tests. It should be easy to bake this into an AngularJS directive for reuse.

 

jsFiddle: ngList and ngPluralize AngularJS Example

These two directives individually are pretty awesome, but they also go together really well, at least for demonstrating features. ngList converts string lists into arrays, while ngPluralize converts arrays into different strings based on the item count in the list. Both are important to know.

 

jsFiddle Example: Mustache Directive for AngularJS

In the blog post Considering Speed and Slowness in AngularJS, the idea is put forward to use MustacheJS, wrapped in an AngularJS Directive to render dynamic content, but come away with no or minimal bindings. I liked the idea, but I found the application too specific; while I get the philosophy of making Directives specific usage, I also hate redundant tools and making a separate directive for each and every place I want to use a Mustache template seems terrible too.

The key detail to note in this example is that the first element wrapped by my directive is a div with a ng-non-bindable on it. That is necessary to keep AngularJS from trying to parse and bind your Mustache template code.

 

More Entries

Jon Hartmann, July 2011

I'm Jon Hartmann and I'm a Javascript fanatic, UX/UI evangelist and former ColdFusion master. I blog about mysterious error messages, user interface design questions, and all things baffling and irksome about programming for the web.

Learn more about me on LinkedIn.