Archive for the ‘Engineering’ Category

Great Hack: Writing an MVC Framework From Scratch

June 4th, 2013 by Shaun

Overview

MVC frameworks are a design pattern commonly used when programming modern Graphical User Interfaces (GUI).  There are dozens of well-written, well-supported frameworks out there, so it is not to say that the world needs yet another one, but creating a new one is an excellent exercise in architecture, planning, and testing. That is why my most recent Hack Week project was to design and build my own MVC framework for use in my own projects.

 

Photo credit: Wikipedia.org

 

What is a MVC Framework?

An MVC framework stems from the popular object-oriented programming paradigm called Model View Controller. The strength of this pattern can be easily summarized by the fact that it ensures the separation of concerns, where each individual component is responsible for a specific set of functionality; in this case a model is tasked with managing and storing data, a view is responsible for rendering the visual (display) aspect of the site, and a controller is essentially the brain, taking user input from the views and manipulating data on the model. None of these components should be tightly coupled, providing an environment of interchangeable pieces.

Wraith v0.1.0

 

Meet Wraith

Wraith was a project I thought up several months ago out of my frustrations with the current MV* frameworks available on the internet. I was working on a few small, single page applications and was testing different frameworks to see which suit my needs. I used Backbone, Spine, Angular, and a few others that didn’t quite fit the bill. What I wanted was a framework that bound the data to the view, something I call implicit model-view binding, but required no logic to be present inside the views.

For all intents and purposes, Angular does provide this level of functionality, and so does Backbone, with the help from a variety of different plugins. But Angular is rather big, has a pretty steep learning curve, and doesn’t enforce logicless views (something I feel is extremely important in such a framework), and Backbone takes a bit of finagling to get anything to work quite right. Additionally all of these frameworks work best when used with a library like jQuery, or Zepto to handle event delegation and DOM manipulation.

Why make another MV* Framework?

I wrote Wraith because I wanted a MV* framework that didn’t depend on any external libraries, had Angular-like Model-View binding, and was super lightweight and easy to understand. Additionally I wanted to write this framework in CoffeeScript since it is easy to read, has powerful array comprehension, and is just a ton of fun to write in.

Along the way I sought inspiration from Spine, Backbone and Angular, mixing Spine-style Models and Collections, Angular style directives, with Backbone-style templating (a la Handlebars). All of these inspirations make Wraith a unique experience, but it still feels incredibly familiar to most frontend developers.

What makes Wraith different?

Wraith is completely self-contained. You need nothing else to get started creating a basic single page application. I say basic because the framework is very much in its infancy. It does not have support for URL routing, AJAX requests, animations, or persistent storage. These are all things I hope to accomplish in the near future.

Now that I have identified what Wraith doesn’t have (yet), lets talk about what it does well:

  • Implicit Model-View binding (akin to the MVVM design pattern)
  • Controllers that are also views (again, MVVM)
  • Handlebars-esque logicless templating
  • Template declaration directly in the DOM that doesn’t require a compilation process
  • Event binding directly from the DOM, instead of requiring JS to do so
  • Partial view updating (only update elements that changed)
  • Well under 20kb when minified

How to get started with Wraith?

Wraith is declarative, in that much of the heavy lifting – data and event binding, class and text manipulation – happens directly in the markup (HTML). Your controller is initialized from the DOM directly, so when you create your app it’ll look something like this:

<section data-controller=”App.MainController”></section>

 

Wraith will require you to create an App.MainController object in the global namespace, which it will find and create an instance of, binding it to the element that its defined on (in this case, section).

CoffeeScript:

App = {}
class App.MainController extends Wraith.Controller
  constructor: ->
    @registerModel(App.List, 'list') # Register our model as 'list'

  onKeypress: (e) ->
    alert(e)

 

JavaScript:

var App = {};
App.MainController = (function(_super) {
  function MainController() {
    _super.call();
    this.registerModel(App.List, 'list') // Register our model as 'list'
  }
  MainController.prototype.onKeypress = function(e) {
    alert(e);
  }
  return MainController;
})(Wraith.Controller);

 

Before Wraith will do anything though, you must initialize its bootloader. This will start the controller initialization.

<script type=”text/javascript”>
  new Wraith.Bootloader(); // My app is starting!
</script>

Event Handling

In Wraith, you are required to create event handlers in your controllers, but you bind them to events inside the DOM structure like so:

<section data-controller=”App.MainController”>
  <input type=”text” data-events=”keypress;onKeypress" />
    <div data-bind=”list.items” data-repeat>
      {{text}}
    </div>
</section>

Now when the text input is typed into, the onKeypress method on App.MainController will be invoked.

Models and Collections

I really enjoyed working with Models in Spine when compared to other frameworks, and thus Wraith’s models are similar in design. You can create a new model with default values easily:

CoffeeScript:

class App.ListItem extends Wraith.Model
  @field 'text', { default: 'New Item' }
  @field 'selected', { default: false }

 

JavaScript:

App.ListItem = (function(_super) {
  function ListItem() {
    _super.call();
    this.field('text', { default: 'New Item' };
    this.field('selected', { default: false };
  }
  return ListItem;
})(Wraith.Model);

 

Collections can be done similarly:

CoffeeScript:

class App.List extends Wraith.Model
  @hasMany App.ListItem, 'items'

 

JavaScript:

App.List = (function(_super) {
  function List() {
    _super.call();
    this.hasMany(App.ListItem, 'items');
  }
  return List;
})(Wraith.Model);

Data Binding

One of the most important things I tried to accomplish with Wraith was easy data binding. I didn’t want to write logic in my views, so I needed to handle looping over collections as well as showing and hiding views or partial views. The solution was to allow a view to be bound via dot-notation to a property on a model similar to what Angular does.

<section data-controller=”App.MainController”>
  <input type=”text” data-events=”keypress;onCheckboxKeypress” />
  <div data-bind=”list.items” data-repeat>
    {{text}}
  </div>
</section>

 

This will bind the input to the list property on your controller (App.MainController). Every time the list.items property changes, the view will automatically be updated (and in this case, repeated as a list).

Class Binding

Want to hide or show something? Instead of writing logic in JavaScript to hide and show an element or alter its class attributes, you can use data or methods from your models to alter the class structure.

<section data-controller=”App.MainController”>
  <input type=”text” data-events=”keypress;onCheckboxKeypress” />
  <div data-bind=”list.items” data-repeat>
    <span data-class=”highlight:selected”>
      {{text}}
    </span>
  </div>
</section>

 

When selected is true, the class highlight will be applied to the span surrounding our text.

Want to know more?

This was just a brief overview of what Wraith is capable of doing right now, and what it will be capable of doing in the future. For more information, follow me on Twitter (@Shaun_Springer), check out the github repository, read the documentation, and check out the examples below:

Illuminating Hack: Pager Hue-ty

May 31st, 2013 by Justin

As many of you guys know, every 7 weeks at Chartbeat we have a Hack Week where we can work on any project or projects that interest us. At the end of the week, we present our creations, some of which make it to our Labs page or even become fully-fledged features for our products. 

Meet Hue.

Last year Philips released a much-hyped LED light bulb called the Hue that gives you complete control of every aspect of the bulb from your smart phone or through the Philips website. Hue lights have an API which unlocks a ton of potential for interesting integrations.  I acquired a Hue set around the holidays and ever since I’ve been wanting to hook them up to our PagerDuty alert notification system at Chartbeat.

Enter: My Hack Week project, which I’ve named “Pager Huety”.  Pager Huety is a script that controls the Philips Hue light bulbs based on triggered incidents from the PagerDuty API.

Meet me and my hack.

Part of my job as the Chartbeat Senior Web Operations Engineer requires being a part of an on-call rotation to ensure everyone’s dashboards are running smoothly 24/7.  Occasionally you may see an issue on your dashboard but luckily we’re immediately alerted to any issues via our monitoring system, which will send an alert via PagerDuty to the dedicated on-call team member.

I wrote Pager Huety to wake me up in the middle of the night with my Hue lights rather than have my phone going off with whatever less-than-awesome ringtone I’ve selected.  There’s an option to only have it alert during nighttime and filter for incidents only assigned to me.  Currently the default light sequence is to flash the bulbs red and blue a few times, then turn bright white for 10 seconds and shut off.  A video of Pager Huety in action can be seen below:

 

There are a lot of cool things you can do with the Hue Bulbs and your Chartbeat data.  You can even utilize the Chartbeat API to flash Hue lights when you hit a new 30 day max – want to give it a go yourself? Tweet your results to @Chartbeat.

Chartteam Spotlight: Shaun Springer, Frontend Engineer

April 18th, 2013 by Cat

Whoa! I know what you’re thinking. You want more of those crazy-interesting Chartteam spotlights and this second round is long overdue. Don’t fret, don’t fret — we’re about to get up, close, and personal with Shaun, a Frontend Engineer extraordinaire, who joined us early this year. As far as I can tell, he’s happy here – though he does frequently complain to me about how the insane amount of cake and desserts is affecting the fit of his pants, but that’s one of the better problems to have in my opinion.

You may have noticed that Shaun’s been all over our blog recently, sharing some of the work he’s been up to. I sat down with him a few days ago to chat about his experience at Chartbeat so far and what keeps him excited about what we’re building here.

What he brings to the table:

Shaun didn’t necessarily go to college with his mind set on frontend engineering – he actually went to school for Electrical Engineering. Before coming to Chartbeat, Shaun owned a paintball company, travelling the world playing in awesome leagues like PSP and AXBL. Yes, I said paintball company. Most recently, though, he spent some time at Crestron Electronics as the head of UI for their software group and as a Lead Frontend Developer for Lot 18.

image-1 shaun-1

So what cool things are you working on now?

My main focus this cycle includes tying up loose ends on the Video Dashboard as well as building the Ooyala Flash and HTML5 video plugins. However, I’m most passionate about the work I’m doing around measuring content quality – or at least trying to. I’m working on my Shaun Appreciation Score ( it’s not a narcissistic naming choice by any means), which is my attempt to measure how much time users are spending inside actual content – the stories themselves – and not just on the page. If you’re curious, check out my most recent post. 

What challenges are you facing as we continue to grow?

In the short term, I’m looking at creating a pinger that is optimized for size since serving billions of requests off of our CDN can get real pricey. What’s even more interesting is that the pinger needs to work with lots of different use cases like Brightcove, Flash, etc.

 If we’re talking about long-term challenges, that’s harder to say because we’re working on so many cool things right now and only time will tell, really. I suppose I’m most focused on creating fast, responsive UI’s that are user intuitive and fun to use.

What have you learned working here?

One of the greatest things about working at Chartbeat is how much you’re encouraged to continue learning. At Chartbeat, I work with some of the smartest people I’ve ever worked with,  so I’ve been pushing myself to new limits lately. Specifically, I’ve learnt a lot more about Python over the last few months and also doing frontend work that’s optimized for scale (because we’re growing!).

What’s the weirdest Chartbeat moment you’ve had so far?

Our CEO Tony and his wife Maya get to know new hires by cooking dinner for them. The first thing I noticed when I arrived at their place was their massive book collection and more importantly, this enormous number of romance novels on the shelves. Two minutes later, everything clicked and I realized that Maya is a romance novelist by trade and I no longer felt the need to judge Tony’s less-than-manly reading preferences. ;) 

What do you think one of the best Chartbeat perks are?

A culture that supports dogs in the office isn’t an uncommon perk for most startups out there, but Chartbeat is awesome about pups at work. I may be a little biased around this perk because I think my dog, Ranger, is the best, but the Chartpups really make our office fun, relaxed and happy.

image

I love it so much that every Wednesday I try to wear my “Champ” tee since Champ is in the office most weeks on that day. It’s amazing how something so simple makes long days at work far more enjoyable for everyone.

Clearly, we have some amazingly challenging problems that come up every day that only the brightest – and quirkiest – minds out there can solve.

If you’re a data-loving nerd at heart and curious about why frontend development at Chartbeat is awesome, get in touch with Shaun directly or check out one of our openings. We’d love to meet you.