Sunday, December 23, 2012

Mobile Single Page Application

Intorduction

A few days back I’ve started to think about creating a mobile SPA at work. Now, we work with jQuery Mobile to create mobile apps. jQuery Mobile has some sort of a behavior that can make your website look like a SPA website.

Now, jQuery Mobile looks at each html page for div with data-role “page” and every time one navigates to another page, the next page retrieved by AJAX request and the “page” div is gets injected into the DOM. It also adds a transition animation and thus it looks like the page is changing on-the-fly. There are a couple of potential problems with this technique:

  • The whole html is retrieved per page request resulting in high-bandwidth calls which actually has impact on the amount of data used (which is limited to a couple of GBs per month) and on the speed.
  • You cannot add scripts in the next page outside the “page” data-role div because it won’t get injected to the main page and so won’t exist. Also, the jQuery document.ready event will only fire once on the initial page request, although jQuery Mobile provides another method to deal with this problem (e.g. pagecreate) it’s less convenient.
  • Since more than one page gets injected into the same html document you need to be sure not to use same id attributes in two different pages because it will create more than one element with the same id and will create id conflicts on the DOM.

There is one thing though that in my opinion works very well in jQuery mobile. The mobile UI and layout. It is relatively easy to create and customize the UI with jQuery Mobile. Also, you do not need to worry about device-width issues, since they are resolved automatically by the framework.

So to start with something I had to choose libraries to work with.

So what do we need?

We work extensively with knockoutJS to gain the MVVM behavior in javascript. This framework looks mature enough so no reason to skip it. Now, knockout will also give the ability to use templating ability to dynamically display data. One limitation with the knockout templates is that you can only place them in the same html file they are used in. To handle this we can use another library which, can be found here.

For additional info please visit knockoutJS website

We will also need routing engine. Two libraries were considered:, sammyJS and pathJS. And of course the jQuery Mobile framework to get the mobile look & feel. So, we end up with the following:

  • jQuery Mobile
  • knockoutJS
  • pathJS
  • external template engine

After trial and error I was able to create a small SPA example using all of this component.

There are good MVC client side frameworks for SPA like backbone or ember. I did not try to reinvent the wheel. The purpose of this example was to check if we can use simple matured libraries we already use on daily basis in order to create simple SPA without suffering from high learning curve which exists in other frameworks.

The full example is included in the following git.

No comments:

Post a Comment