Sample App with Backbone.js and Twitter Bootstrap

Backbone.js is a lightweight JavaScript framework that provides the basic infrastructure (Model, Collection, View, and Router classes) to bring structure to your Web applications.

Twitter Bootstrap is a UI toolkit that provides simple and flexible HTML, CSS, and Javascript to implement popular user interface components and interactions.

In other words, Backbone.js and Twitter Bootstrap focus on different areas of your application: core architecture and user interface respectively. Because of their well-defined and non-overlapping scope, Backbone.js and Twitter Bootstrap work well together. In general, I find a lightweight architectural framework and a UI toolkit to be a powerful combination, and an interesting alternative to full-stack frameworks: it gives you the flexibility to choose the library you like (if any) in the respective areas of your application.

The Sample Application

To give this combination a try, I put together a new sample application that uses Backbone.js to organize the code, and Twitter Bootstrap to organize the UI. The application is an Employee Directory that allows you to look for employees by name, view the details of an employee, and navigate up and down the Org Chart by clicking the employee’s manager or any of his/her direct reports.

You can run the application here.



Backbone Directory is a single page application: index.html is essentially empty. Views are injected into and removed from the DOM as needed. Even though it is a single page application, the Backbone.js Router makes it easy to keep the different states of the app “bookmarkable” and “deep-linkable”.

Twitter Bootstrap highlights

“Backbone Directory” uses a number of the Twitter Bootstrap styles, components, and interactions: the 12-column grid with nested columns, a “Navbar”, a “Search Form” with dropdown, the dropdown plugin, the Glyphicons icons, Info and Warning alerts, a “Well”, etc.

Backbone.js highlights

If you are new to Backbone.js, you may want to start with the tutorial (part 1, part 2, part 3, and postface) I blogged recently. “Backbone Directory” includes some interesting elements not covered in the tutorial:

  • One-to-Many association. A one-to-many (Manager-to-Employees) association is defined in the Employee model (model/employeemodel.js) as a collection of employees (the direct reports). That collection is lazily fetched in the render() function of EmployeeFullView (view/employeedetails.js).
  • Composite View. EmployeeFullView (views/employeedetails.js) is an example of a composite view. Its render() function instantiates two subviews: EmployeeView and EmployeeListView (to display the employee’s direct reports).

Source Code

The source code is available in this repository on GitHub.

Your feedback and comments are appreciated.

Comments

  1. What server scripting languages and frameworks have you used with backbone.js? Have you found it to be backend agnostic, or does it work best with node.js, python, ruby, php, etc?

  2. Not sure if it’s a bug with Backbone or the app, but if I click to any other page other than the homepage and try to go back and click the “Show Me!” button it doesn’t bring up the list of employees.

    Also, does Backbone not support pushState for history? I don’t know much about this library and am just starting to get into it.

    Great little app though! Thanks.

    • Masklinn says:

      Backbone has optional support for pushstate. It is not enabled by default because not all browsers handle it yet (iOS Safari pre 5.0 is buggy, IE9 still does not support it, not sure about the Android browser and Safari may have issues there as well).

      You can enable `pushState` support by passing in the `pushState: true` option when starting the history handler: `Backbone.history.start({pushState: true})`

  3. JoshLeaves says:

    Thanks! I’ve been looking for a nice BackBone.js starting point for quite some time. Your source code should be a good help for my next project :)

  4. Motyar says:

    Good work COENRAETS, thanks for sharing this.

  5. Dan says:

    Was looking for something exactly like this! Am playing around with Bootstrap but it’s all UI. This is awesome!

  6. Christophe says:

    @Patrick: Yes Backbone is backend agnostic. For access to remote services, it works really well with RESTful services by default. But you can override Backbone.sync to implement any kind of synchronization strategy. See this post for an example of using the local database on a local device.

  7. Dan says:

    Seems to not want to render in IE 9 (I know, I know, BUT many big companies use it). I can “view source” and the source loads – just no rendering happening… looks great in chrome.

  8. Sandeep says:

    Looks great in Chrome as previous poster said. Doesn’t seem to work in FF 3.6 though. Do you know if this is because Bootstrap doesn’t support it?

  9. Hamund says:

    Great to see that the HTML+CSS+JS workflow is improving. However, I cannot help to think that this “app” would feel smoother in Flash. I think it has to do with small details such as how dropdown lists are rendered, how the page resizes when the elements are loaded, how the hoover selection in the dropdown feels a bit too instant etc. I guess this can be improved by tweeking the css, but these things are natively done right in Flash.

  10. Kory says:

    Nice tutorial. I was wondering how I would go about not showing a particular piece of an html template, is that possible? For example in employee-details.html how could I conditionally show or not show the manager field? Normally I would surround that in an if block with my server side scripting language but with backbone I don’t understand how to accomplish that. Any suggestions?

  11. Alan says:

    Great work Christophe. I’ve been getting to grips with Twitter Bootstrap 2.0 for an internal work tool the last couple of weeks and this sample is well timed.

    If you’re stuck for your next sample a backbone.js and Twitter Bootstrap CRUD app would be great ; )

  12. Daniel says:

    The app is great but there are two problems that I found.

    1: the contact link doesn’t get the focus when clicked.
    2: if I click one of the top menus, home or contact, the show me doesn’t work, I have to remove the # from url and click again.

    I’m gonna try that application with knockout.

    Thanks for you time Christophe

  13. Great information. I’ve been searching a long time for something like this.
    I should start now learning the aplication with the tutorial and your “Backbone Directory”, i’m new…hahaha
    Thanks a million!

  14. Christophe says:

    @Daniel & Esteban: Thanks for reporting this. I’ll post an update with minor fixes along with the Mobile version.

  15. Benj says:

    Hi Christophe,
    Really nice App. Thank you. I am a beginner and it will help to start learning backbone and Bootstrap. I have some ideas I will try to learn and do my webapp on my own. I am wondering why you didn’t commercialize this. Small companies can buy this right and is very useful for them just like any time tracking app.

    Need small Info: Based on backbone+Bootstrap, are there any open srcd/example templates where a simple app that has support for user login/signups. I want to create an app that allows user to login and enter some details for app to work. One cant access others data. Any pointers how to go about ?? Any one else can also help me.

  16. Thanks for all the educating posts!

    Looking forward to the mobile update and maybe some thoughts on how this Bootstrap approach compares to the one with jQuery Mobile?

  17. Thanks! What’s the license for your project ?

  18. Carlos Martins says:

    Using this url patterns do you believe that google indexation will work fine?

  19. mohsen says:

    I wrote Perl backend ( using Mojolicious ) for your sample, let me know if you would like to add it to source code. Thanks for sample code

  20. James Brown says:

    Christophe, I found a minor bug:

    If you visit the main app: http://coenraets.org/directory/

    Click “Show Me” under “Try It”. Select someone. Click back. Then click “Show Me” again… nothing happens.

    Very minor but FYI

  21. snick says:

    You know what would be really cool?

    An extended version of this tutorial w/ additional login and admin capabilities.
    One of the biggest question to newcomers on rest-like architecture is how handling login and performance security operations (like adding or removing contents, auth, and so on).

    I’m sure it will be a great follow up article
    Best
    snick

  22. Ernesto Pino says:

    I wonder how I can do to mount this tool locally (localhost). Right now I work, if not due to .htaccess is not set in my MAMP. Or is that the folder “api” PHP is not well located within the project and is not accessible.

    Thanks.
    PD: Sorry for my English.

    • Tom says:

      Hi
      Did you manage to solve this problem as I appear to have the same issue? – does it have to be on live server?

      Cheers

  23. Dimitris says:

    Waiting for a new backbone.js article.!!!!

  24. BackboneGuy says:

    Hey,
    This is amazing! Thank you for your work.

    This uses Twitter Bootstrap 2 right?

    Regards

  25. leon says:

    Amazing example of using backbone.js.

    A little confuse about property model of EmployeeListView in “employeelist.js” file.
    I think it is better to name it as collection. Actually, it is assigned an instance of EmployeeCollection in “header.js”.

  26. doug says:

    did you find more UI flexibility with Bootstrap over JqMobile? or the other way around?

    have you tried compiling a twitter bootstrap mobile site through phone gap

  27. Nambi says:

    I’m totally new to javascript, twitter-bootstrap and backbone. This is a great tutorial to get started with these.
    Read http://backbonetutorials.com/ before starting this tutorial, it will explain the basics of backbone.

    btw, how did you create the image that shows the employee hierarchy?

  28. BillSaysThis says:

    Chris,

    Thanks for this and the other great Backbone code and articles. One thing I’d love to see you cover that I haven’t found a good tutorial for yet is handling Bootstrap’s modals, tooltips and popovers–is this something you plan to cover soon?

  29. Arnaud says:

    I solved 2 bugs in this great application:
    1. the first one is the “Show Me” button bug : if you click on Contact, then on Home and on the “Show Me” button, the search popup does not appear. This is due to the view recycling system: events are lost when you reinject the HTML into #content. To solve this, you can call the delegateEvents() method of the View.
    So in the main.js file, change the home function as this:
    home:function () {
    // Since the home view never changes, we instantiate it and render it only once
    if (!this.homeView) {
    this.homeView = new HomeView();
    this.homeView.render();
    } else {
    this.homeView.delegateEvents(); // delegate events when the view is recycled
    }
    $(‘#content’).html(this.homeView.el);
    },

    2. I found another bug with the search field: if you press Enter in this field, the page is reloaded!
    Quite easy to solve: in the header.js file, add the keypress event and filter the Enter key:
    events:{
    “keyup .search-query”:”search”,
    “keypress .search-query”:”onkeypress”
    },

    onkeypress:function (event) {
    if (event.keyCode == 13) {
    event.preventDefault();
    }
    }

    Hope this helps!
    And many thanks to Christophe for all your great tutorials!

  30. Luiz Geovani Vier says:

    Very nice sample. Thanks!
    I’ve created a node.js backend in case anyone is interested: https://github.com/lgvier/directory
    Cheers

  31. nice example using backbone.js+Bootstrap.css+Slim.php.
    i hope to see amdjs version….
    regards.

  32. Tom says:

    Hi
    First of all – a big thanks for this tutorial – it’s set me on the right path.

    …but one problem. I have downloaded the app and trying to get it going on my MAMP setup. I have also imported your directoy.sql file into a ready made database and everything seems to work just great. The problem arises when I select an employee from the drop-down list. It sets the correct URL but nothing shows and I’m not sure why. Obviously I’m connecting to the DB ok as the people wouldn’t appear in the drop-down list so not sure where to start looking.

    Any help from anyone would be much appreciated.

    Thanks

    • Tom says:

      Sorry – I should be more specific. When I select an employee from the drop-down list it just stays on the homepage but the URL changes in the address bar! I’m thinking it might be to do with the fact I’m on a local setup???

Trackbacks

  1. [...] Sample App with Backbone.js and Twitter Bootstrap – [...]

  2. [...] Sample App with Backbone.js and Twitter Bootstrap – [...]

  3. [...] and it lets you use the UI toolkit of your choice or simply roll your own styles and widgets. In my previous post, I demonstrated how to use Twitter Bootstrap on top of [...]

  4. [...] Backbone.js is a lightweight JavaScript framework that provides the basic infrastructure (Model, Collection, View, and Router classes) to bring structure to your Web applications. Twitter Bootstrap is a UI toolkit that provides simple and flexible HTML, CSS, and Javascript to implement popular user interface components and interactions. In other words, Backbone.js and Twitter Bootstrap focus on different areas of your application: core architecture and user interface respectively. Sample App with Backbone.js and Twitter Bootstrap [...]

  5. [...] good? For traditional web apps (delivered through a browser), Twitter Bootstrap can help (read here). But what about Mobile apps? I explored Backbone.js + jQuery Mobile here. Depending on what you [...]

  6. [...] to be a big thing and should be read-only. Just as a simple employee directory. I came across http://coenraets.org/blog/2012/02/sample-app-with-backbone-js-and-twitter-bootstrap/, this sample app look like what I'm searching. But they use MySQL (yes I could try to dump our AD [...]

  7. [...] | Framework Get flash to fully experience Pearltrees 004 JSJ Backbone.js with Jeremy Ashkenas Sample App with Backbone.js and Twitter Bootstrap In other words, Backbone.js and Twitter Bootstrap focus on different areas of your application: [...]

  8. [...] few weeks weeks ago, I posted a first Backbone.js and Twitter Bootstrap sample application. While interesting, “Employee [...]

Speak Your Mind

*