Getting started with Ember.js

January 31, 2013

Here's a quick guide to some code which accesses a redis database via webdis and then displays all the keys it finds. It also provides an option to delete keys from the database. In these code examples, webdis is accessed at http://localhost:7379/


We're using webdis to access redis, which returns information about the data in redis using JSON.

http://localhost:7379/LIST/* lists all keys in Redis.

{"KEYS": {"KEYS":["key2","key","key1"]}

http://localhost:7379/GET/key returns the value of a key.


http://localhost:7379/DEL/key deletes a key



This first bit of code in app.js creates the application. It also includes a callback that runs when the page is ready, just like jQuery.

App = Em.Application.create({
  ready: function() {
      // Run when the page is ready 
}) ;

Next we define an object to hold our key/value pair

App.KeyValue = Em.Object.extend({ 
   key: null,
   value: null

Next we define our controller. This will manage a set of KeyValue instances.

content is the list of KeyValue instances

deleteKey is a function run from the view when the [delete] link is clicked on the page.

loadKeys is a function run from the application ready callback to load up the initial list of key/values from redis.

App.configController = Em.ArrayController.create({

  content : [],  // holds list of Key/value pairs

  deleteKey : function(view){ // called from view to delete key
    var me=this; // Get hold of this (it changes in the callback)
    var delUrl = 'http://localhost:7379/del/' + view.context.key;
    $.getJSON(delUrl,function(pDeleteResult){ // Remove key from Redis
      me.removeObject(view.context); // remove object from content collection

  loadKeys: function() { // Load all keys from redis
    console.log("Loading keys...");
    var me = this;

    var keysUrl = 'http://localhost:7379/keys/*';
    var keyUrl = 'http://localhost:7379/get/';

      me.set('content', []); // clear out existing keys
      $(pKeysData.keys).each(function(index, pKeyName){ // for each key
        var lKeyValue;
        $.getJSON(keyUrl + pKeyName,function(pKeyData){ // get key value
            lKeyValue = pKeyData.get

            // Create object with key/value pair
           var t = App.KeyValue.create({ 
                key: pKeyName, 
                value: lKeyValue

            me.pushObject(t); // add object to list this object holds


The loadKeys function fetches all the keys from Redis, and then loops through the keys returned fetching the value for each key. It then creates a new App.KeyValue instance with the appropriate key and value and then adds that new object to the list stored on the controller.


The HTML file loads jQuery and ember (ember depends on jQuery) and then loads the app.js file.

Next we use Mustache template to loop through the KeyValue pair objects on the configController, outputting the key and value attributes into the HTML.

We then have a delete link which calls the deleteKey() function on the configController.

<!DOCTYPE html>
   <title>Redis Keys</title>
   <script src=""></script>
   <script src=""></script>
   <script src="app.js"></script>
   <script type="text/x-handlebars">
      <div id="content">
         <div id="tweets">
              {{#each App.configController}} 
                <p>{{key}} = {{value}} 
                    [<a href="#" title="delete this key" {{action "deleteKey" target="App.configController"}}>delete</a>]</p>

A major point to note about this implementation is that it uses data binding between the list of key/value pairs shown on the web page and the list updated from the JavaScript. When the call to me.pushObject(t) is made, this adds a new object to the configController list and it is automatically shown on the page. The same happens when we delete key/value pairs - we call out to Redis to actually delete it, and then delete it from the configController. The display is always automatically updated.