July 2012 Blog Posts

jQuery.each and Object Boxing

I just spent like six hours trying to figure this out, so profit from my experience.

I have some custom code wiring up jQuery Validation in a form and for some reason, the jQuery "html" method was causing a "HIERARCHY_REQUEST_ERR: DOM Exception 3" whenever validation errors were being displayed. After banging my head against it for a while and getting some help from a co-worker, we found it came down to the $.each iterator.

I was compiling up a list of error messages and using the $.each to add them to settings for a jQuery validator. It looked something like this:

var parsed = {
  required: "A is required",
  notEqual: "A must not equal B"
};
$.each(parsed, function(rulename){
  options.messages[prefix + rulename] = this;
});

As you can see, I wasn't doing a straight extension - I needed to add a prefix to the hash key, so I iterated through. The gotcha is that this in the iterator loop isn't actually literally the same as the value of the item.

More explicitly:

var parsed = {
  required: "A is required",
  notEqual: "A must not equal B"
};
// typeof parsed.required === "string"

$.each(parsed, function(rulename){
  // typeof this === "object"
  options.messages[prefix + rulename] = this;
});

That type conversion was what was causing the problem. Way down the line, after wiring everything up to jQuery Validation, the validator would try to do something roughly like this:

$("<span />").html(message)

At the time of execution, message wasn't a string, it was an object, so the DOM threw a tantrum and resulted in "HIERARCHY_REQUEST_ERR: DOM Exception 3".

The answer is to not use this in the iterator. Instead, provide the second parameter to the iterator function that brings along with it the original item value:

var parsed = {
  required: "A is required",
  notEqual: "A must not equal B"
};
$.each(parsed, function(rulename, message){
  options.messages[prefix + rulename] = message;
});

That way the string value isn't boxed into an object and you won't lose the type information.

Quick Overview of Samsung TecTiles

I recently upgraded my phone to a Samsung Galaxy SIII. So far, seriously, this thing is awesome. It's fast, I love Android 4.0, the screen is super clear, the camera is nice... serious win over my older Droid X.

One of the things Samsung seems to be pushing is the NFC (near field communication) abilities of the phone. They have these little NFC tags you can buy called "TecTiles" that you can use to play with NFC. They have an Android app you can use to program the tags or use them to execute programs from tags read by your phone.

I figured I'd try out some of these TecTiles and see what it's all about.

TecTiles are small, about an inch square, and are stickers. Here's one still on the backing paper:

Samsung TecTile NFC sticker

They run roughly $3 each - you get them in a pack of 5 for about $15, though sometimes you can find them a little cheaper.

To program them, you first open the TecTiles app.

Samsung TecTile app: Pick a TecTile Type

From there, you can pick what you want the TecTile to do. You can choose from "Settings & Apps" (have it change phone settings or launch an app); "Phone & Text" (have the phone make a call, send a text, or add a contact); "Location & Web" (do a Facebook or Foursquare check-in or visit a web site); or "Social" (update Twitter or Facebook status, connect on LinkedIn).

For this walkthrough, I'll choose "Settings & Apps." Once you do, you get asked what sort of tag you want to make.

Samsung TecTile app: TecTile Action

I chose "Change Phone Settings." Then you get to choose which settings you want to change.

Samsung TecTile app: Change Phone Settings

You can select more than one setting at a time, but for now I just chose to set my phone in Silent mode. The next screen gives you some options about what exactly you want it to do.

Samsung TecTile app: TecTile setting values

In this case, I want the tag to set my phone into Silent mode. This is something I might put on my desk at work so when I get in I can scan the tile and have it in "work mode." Once it's all set, you're ready to program the TecTile.

Samsung TecTile app: Write TecTile

You just hold the phone over the little TecTile sticker, magic happens, and it's programmed. You'll notice there are options to "lock" the TecTile so you can't re-program it. If, for example, you put your business contact info on the TecTile and stuck that on the front register at your store, you'd lock the tile so no pranksters overwrite it. There's also an "allow multiple writes" option to allow you to re-use the program across multiple TecTiles. (I was actually hoping this option would allow me to make more complex programs by writing multiple "tasks" to the same TecTile, but, alas, no.)

To use the TecTile, you just sit your phone next to it/on it. Depending on what the TecTile is programmed to do, sometimes it brings up some info directly, but generally it seems to do most actions through the browser. For example, when you update Twitter status, it does it through the browser inteface, not the Twitter app. Here's the weird bit - in the case here, where I'm setting my phone to silent mode, you still get an alert that the browser is launching:

Scanned TecTile: launching the browser

But when I click "OK," the browser doesn't come up - instead the TecTile app takes over and notifies me my phone is going into Silent mode.

Samsung TecTile app: Reading TecTile

Then that screen disappears and I go on my merry way.

I liked that it was pretty simple to get working and seemed novel... but I have to admit I'm a little disappointed. The list of available actions right now is pretty small:

  • Settings & Apps
    • Change Phone Settings
      • Alarm Time
      • Alarm Switch
      • Car Mode
      • Night Mode
      • Bluetooth
      • WiFi
      • Silent
      • Ringer Volume
      • Music Volume
      • Automatic Brightness
      • Brightness
    • Launch an App
    • Join a WiFi Network
    • Show a Message
  • Phone & Text
    • Make a Call
    • Send a Text Message
    • Share a Contact
    • Start a Google Talk Conversation
  • Location & Web
    • Show an Address or Location
    • Foursquare Check In
    • Facebook Check In
    • Open a Web Page
  • Social
    • Update Facebook Status
    • Facebook Like
    • Tweet a Status
    • Follow a Twitter User
    • Connect on LinkedIn

I mean, that doesn't look small, but the first thing I wanted to do was set my phone in Vibrate mode since I use that at work, not Silent. But... no luck. Then I thought it would be neat to do a Latitude check-in since I use Latitude, not Facebook or Foursquare. Nope.

The "Send a Text Message" action doesn't work right now, which is the first one I actually tried (after not finding Vibrate mode or Latitude check-in) but it seems they're having problems with that. The most recent update says it fixed some problem with sending messages, but it doesn't look fixed to me. (You scan the tile and then... back to the home screen. Nothing.)

Another thing: You can't do any complex actions. For example, if you wanted a tile at work that turns your phone to Silent and then checks in... nope. That's two tiles, not one.

In the end, I don't really feel ripped off or anything. I'm having fun messing around with them, and I know I'm pretty early to the party on this, so I have faith they'll beef up the offering as time goes on. However, from a practical standpoint, it doesn't do all the stuff I'd like it to.

On a final note, for a more "real world" type of scenario, check out NameBump. These guys make NFC-enabled business cards. Hand your card to a potential client, and they can scan it with their phone to get your contact info in their phone instantly. That's pretty slick and seems to me a great use of NFC. You could do something similar with TecTiles if you wanted - just program the TecTile and stick it to your card. Or, if you're at a trade show, stick it to your vendor badge and hold it out for people to scan. I may have to do this for the next conference I go to. Rather than exchange cards, "Here - scan my badge."

SublimeMSBuild - MSBuild Package for Sublime Text 2

Sublime Text 2 is the first text editor that's actually gotten me excited about text editors(!) for quite some time. It's fast, clean, super extensible... I bought my license when it was still in beta and I've never looked back.

One of the things it was missing was support for MSBuild scripting - syntax highlighting, build execution, and so on. I do a lot of MSBuild work, I wanted to try my hand at Sublime Text 2 extensions, plus I wanted to try out GitHub, so I took the opportunity to roll all of those things into One Giant Project:

SublimeMSBuild - a Sublime Text 2 package that adds support for MSBuild.

Here's a list of the features it includes:

  • MSBuild file extension handling:
    • .proj
    • .targets
    • .msbuild
    • .csproj
    • .vbproj
  • Build system: Execute the currently loaded MSBuild script and capture the results in the output pane.
  • Syntax highlighting:
    • MSBuild keywords and flow-control elements
    • Standard MSBuild tasks
    • C#/VB special project item elements
    • Well-known item metadata
    • Reserved properties
    • Variables
    • Conditional operators
    • Framework support functions
    • Comment blocks
  • Snippets:
    • New MSBuild Script
    • Comment blocks [trigger = c + tab]
    • Self-closing/simple tags [trigger = > + tab]
    • Content/end-tag tags [trigger = < + tab]
  • Autocompletion:
    • Standard/default tasks (e.g., CallTarget, CombinePath, MakeDir)
    • Project file entities (e.g., Target, Choose, Import)
    • Common item definitions (e.g., Compile, Reference, EmbeddedResource)
    • Well-known item metadata references (e.g., %(Item.FullPath))
    • Reserved properties (e.g., $(MSBuildProjectDirectory))
    • MSBuild Community Tasks (if the MSBuild.Community.Tasks.Targets file is imported)
    • C#/VB special project item elements (e.g., Reference, Compile)
    • Supported framework method calls (e.g., $([System.DateTime]::Now))

Installation is simple. There are two options:

  1. Use Package Control. Package Control is a free package manager for Sublime Text 2. You can select the MSBuild package right from the list of available packages.
  2. Manual installation:

Plus, if you want to muck around with it, add features, or even just see how it works, you can hack on it yourself. There are some pointers in the readme.

I'm personally pretty pleased with how it turned out, particularly the autocomplete/IntelliSense support for the various MSBuild tasks. Try it out, I hope you like it as much as I do.

As far as what I learned along the way... The extensibility model on Sublime Text is pretty awesome and easy to work with. I'm not really a Python guy, but I was able to figure it out pretty easily. And GitHub for Windows is a total gateway drug, so props to Phil Haack and crew for delivering that bad boy.

If you don't have Sublime Text 2, what are you waiting for?

Once you have it, download and install the MSBuild.sublime-package.