Showing posts with label single page application. Show all posts
Showing posts with label single page application. Show all posts

Sunday, August 18, 2013

Angular.js example from usergroup

Introduction

At 19/08/13 I've spoke at Gil Fink  UserGroup at Sela about introduction to Angular.js. Below a short overview of the example app presented.

Structure

When building Angular application is is most important to divide it into modules. The current example was relatively small so the modules division was made by different components of the framework (e.g. controller, services, filters, etc..). 
Note, that if the application is large and complex the division to modules should be made by features rather than by components. Each module should then contain everything related to the feature. It will allow us to take the feature to another Angular project with ease.

Please note, there are 5 examples in the main directory from the most basic to the most complex, I'll overview the last as it includes all the other examples as well

Bootstrapping

As an entry point I have the application.js file which defines all the modules available for the projects including the dependencies between them.

window.directives = angular.module('directives', [], function () {
});
window.services = angular.module('services', [], function () {
});
window.filters = angular.module('filters', [], function () {
});

Right after that, the main module configures the routes.

window.application = angular.module('myMovies', 
    ['directives', 'services', 'filters'], function ($routeProvider) {
    $routeProvider
        .when('/', {
            controller: 'mainCtrl',
            templateUrl: 'templates/movies_list_view.html'
        })
        .when('/about', {
            templateUrl: 'templates/about_view.html'
        })
        .when('/movieDetail/:id', {
            controller: 'movieDetailController',
            templateUrl: 'templates/movie_detail_view.html'
        })
        .otherwise({
            templateUrl:'templates/404.html'
        })
});

The $routeProvider has two functions. when - used to declare a valid route either with or without parameters and otherwise - will redirect at any illegal route entered.

Controllers

Two controllers are available one for displaying the movies on the main page and the other for displaying additional details about a movie
The mainController requests the mockService to get the movies data, and also defines event to remove movie on click.
The movieDetailController uses the same service to get movie by id. Note, the movie id is passed as a parameter in url (not as querystring). The controller then reads the parameter with the $routeParams provider.

Services

One service is available. the mockService gets it's data statically from another js file with timeout to simulate a delay.
Because I used timeout function there I need to call $scope.$apply() inside controller to notify Angular about changes to the data. 

        $scope.$apply(function () {
            $scope.model = {
                movie: movie
            }
        });

Normally, when fetching data from server we should use $http service to get the data, the $http service will take care for notifying about changes

Filters

Build-in filters are used in movies list template to filter movie results by the search field. Also, one custom filter is available to convert minutes to more visualisable format.

Templates

Templates for every page is available and are loaded when route gets resolved. Also 404 template is available for the 'otherwise' function.

Conclusion

To create this example I've simplified the structure of the project. The file structure should be a bit different and I hope that soon I'll post a full project example showing the structure as I see it. 
I also simplified the code for the sake of this example, so probably it won't look 100% the same in real life project.

The code examples are available here: http://goo.gl/1xlQFS

I'll be glad for every input you may have about this post or the examples. 

Wednesday, May 22, 2013

AngularJS User Group @ Microsoft Raanana

Yesterday I gave session about AngularJs at Microsoft Raanana. The sessions gave first impression about AngularJS.

You can find the presentation and the code here

Thanks for everyone who came! Hope you enjoyed.

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.