Thursday, April 11, 2013

A Hello World In Ember.js

I saw several blog posts with inaccurate code for a 'hello world' in Ember. Here's code which actually works:

Loading this file gets you an "index" (default) view with a "hi" link, and clicking that link gets you to "hello world!".

I'm a little embarrassed about 'index' as a naming choice, because it's a bad habit, but I got it from the framework. Ember uses 'index' to mean 'default view,' the same way Rails controllers do. This probably came to Ember from Rails, and came to Rails from Apache, but it really has nothing to do with indexing whatsoever. It's one of the few completely cargo-cult elements in Rails, so it's not my favorite part of Ember either.

However, one thing I do want to give Ember credit for, which you can see in this example: this code's concise. In fact it could be a lot more concise; that 'index' route doesn't actually need to be there. Like a lot of things, Ember supplies it automatically.

So this is better:

And also: