Skip to main content

Why did I write a microframework?



I've always been passionate about tools that allow developers makes their lives easier and makes things more fun. Speaking from my agency experience, I've taken the time to do health checks of current developers and analyzed their tool sets. It become clear that there was too much fragmentation between what libraries, APIs, and development approaches -- It's not their fault as each project was being run like a separate silo. To a developer that is being tossed around on to different projects, this is a nightmare. Furthermore, I had this heroic goal of dispelling that nightmare and building a shared vision of a converging toolbox to make us (front-end engineers) faster, more efficient, and reduce the ramp-up time when swapping between different projects/accounts.

At that point, Blast Mojo Framework (v2 rewrite based on jQuery and plugin injection) emerged. We needed a better tool that could provide front-end engineers clear functional separation through implementation silos (controllers). My idea was that all controllers should load asynchronously, never be dependent on each other, as well as, only be mounted when specific contextual elements were on the page (componentized approach to software development). If developers wanted to concatenate all their controllers, they would have the freedom to do so in 'production' mode to indoctrinate a 1-javascript-http-request approach for performance reasons.

In agencies, deployment platforms can be different: sometimes .NET, ATG (Java), PHP, or Python. We needed a way to simply be agnostic to any development environment, which led me to creating a very simple template at the bottom your page:

</body>
<script src="js/lib/jquery-1.5.2.min.js"></script>
<script src="../dist/mojo.js"></script>  
<script src="js/app.js"></script>
</html>

After taking in many opinions from developers across the company about preferred JavaScript libraries, we crystallized the de-facto groundwork library as jQuery. This was more in line with business decision as most of our developers already attuned to its API, as well as, there were many well-tested plugins that could easily be integrated (carousel, modal dialog). Retraining would've caused some serious concerns, as well as, buy-in from a project management standpoint wouldn't have happened including when their projects are already built on top of jQuery.

Inside app.js, you would find the following:

var app = MOJO.create({ baseSrc: 'js/' });

app
  .configure('pluginSrc', 'js/lib/plugins/')  
  .configure('plugins', ['jqmodal', 'jcarousel', 'pubsub', 'tmpl'])

  .map('#login-example', [{ controller: "ExampleApp.LoginController" }])
  
  .start();

It is highly inspired by Sinatra, Express, and SammyJS. MOJO.create() would return you a new Mojo Application instance. configure() allows you to configure your application. In this particular example, we are telling our application to inject "jqmodal", "jcarousel", and "pubsub" -- All three are well respected jQuery plugins and widely used. This approach provides us with more agility over handling plugin dependencies, you won't need to add new script tags or work with a build script to compile everything (but you certainly can, if you want, it's mainly for convenience).

In the next part we are calling map(), which takes a CSS selector as its first argument (which DOM element do you want to map controllers to?) and an array of controllers in the second argument. This will allow to bind multiple controllers to a particular DOM element, useful for sharing functionality, such as, you could map LoginController as well as CarouselController if they have functional commonality.

The next step would be write your controller:
/* 
 * @class   Login Controller
 * @author  Jaime Bueza
 */
MOJO.define('ExampleApp.LoginController', {
  events: [
      ['context', '.btn-login', 'click', 'Login']
    , ['context', '.btn-logout', 'click', 'Logout']    
  ],
  methods: {
    Login: function(requestObj) {
      var context = requestObj.getContextElement();
      alert("Logged in from " + this.controllerClass);
    },
    Logout: function(requestObj) {
      alert("Logged out from " + this.controllerClass);
    }
  },
  after: {
    Start: function() {
      //Initialization
      console.log("Oh hai! I've initialized myself as Login Controller!");
    }
  }
});

In the above code piece LoginController, developers can quickly see how structured they are. The first thing they can see is an events array, which tells them which events are binded to which elements within its context. In this case, the context is the DOM element $("#login-example"). Blast Mojo will instinctively use event delegation so if your controller blows away its child elements, you won't have to worry about rebinding.

I will be writing more about my adventures in building better developer tools and developing better approaches that will hopefully make developers' lives easier. Agencies can be a rough place and if we start rethinking our tool-chains and educate each other on more mature software engineering paradigms, we can react quicker to changes and make things easier in the long-run.

In conclusion, while I understand the Blast Mojo is more suited towards agency-style deployments, it primarily focuses on distributed software development teams (for web, mobile, tablet) that want to leverage common software engineering patterns: implementation silos, dependency injection, aspect-oriented programming, as well as, publish subscribe (messaging). It is indeed a more mature approach to front-end engineering and provides structure for your JavaScript so that when you have teammates swapping in and out of different projects, they'll be able to identify where controllers are and how to fix/implement features instinctively -- And best of all, it is built on top of everyone's favourite library, jQuery!

Good luck and have fun,
Jaime Bueza

Jaime Bueza is a software developer in Vancouver, British Columbia, Canada. He has developed web applications for Nintendo, Starbucks, Bacardi, Nike, Electronic Arts, Ritchie Brothers, Kiwi Collections, Cox Communications and Microsoft. When he's not developing useful software that constantly evolves with business requirements, he's creating tutorial videos for aspiring front-end developers.
Post a Comment

Popular posts from this blog

TextMate Tutorial: How to add a Strikethrough keybind to your Markdown bundle

Markdown is awesome for quickly generating Readme's on Github. After looking at other projects using the strike tag, I've decided to create a custom keybind for it in my TextMate Markdown bundle. Here's how:

1) Click the + sign on the bottom left and click New Command.
2) Paste this into the editbox and make sure you name your command "Strikethrough".

For the input field, select WORD in the drop down.
For the output field, select "insert as snippet".
As for the keybind, you can totally map it to whatever you're comfortable with but I chose Command-D as it is the same thing in Microsoft Word.

Cheers,
Jaime

Using Git Hooks: Prepare Commit Message to automatically prepend branch names on commit messages

When you're practicing branch by feature with distributed version control, typically you'll get assigned a ticket or issue and that ends up being your feature branch. Instead of always typing in the branch name in every commit, you can edit your Git hooks (specifically prepare-commit-msg).

Assuming that this is a brand new git repository:

mv .git/hooks/prepare-commit-msg.sample .git/hooks/prepare-commit-msg
vi .git/hooks/prepare-commit-msg

Edit the file by commenting out what was originally in the file and then add this:



Now, whenever you make a commit, it should show up like this in the log:



Since GitHub and Bitbucket both support Emojis inside commit messages, you can do something cute like this



Want more emojis? check out the Emoji Mardown Cheatsheet!

NodeJS Hack Session: MMO Pokemon with NodeJS/WebSockets

The primary focus of this proof of concept is to determine how easy it is to build real-time web applications for all iPad, iPhone, droids, Safari, Chrome users on top of NodeJS (non-blocking event driven server side Javascript platform). The proof of concept was built within 6-8 hours including the following:

Uses Express framework for server side Javascript development (inspired by Rails / Sinatra / Django)Uses EJS for templating language (much like Django templates and symfony) -- allows partials and passing parameters into partials like symfony PHPReal-time chat using WebSocketsArena Queueing System for real-time competitive matchplay using WebSocketsHTML5 AudioCSS3 transitions for all hand cards, tappable cards, transparent panels, rounded corners, drop shadowsFallbacks for Firefox, IEFirefox/IE will fall back to Flash socketIE will fall back to XHR long poll if the user doesn't have Flash installedNoSQL CouchDB for fetching users and soon cards, achievements, friend associat…