»
S
I
D
E
B
A
R
«
Model-View-Controller (MVC) with JavaScript
Jan 20th, 2010 by franciov

In developing Javascript Applications, the best thing you can do is rely on a framework such as SproutCore (that has recently reached version 1.0) or JavascriptMVC. However in some cases the Dojo toolkit or the jQuery library can satify most of your needs, expecially when you are relatively new to Javascript or just want to avoid the initial learning time associated to the use of a framework. But, as often happens in these situations, the lack of a framework, or at least of standardized and shared solutions, causes problems with maintainability, extensibility and complexity.  This is particularly true for Mobile Widgets, which are relatively simple Javascript Applications which don’t need, at least in my experience, a framework on which rely on, but need to implement architectural patterns and design patterns.

My goal for this article is to show how to create the main structure of a MVC-based Javascript Application using pure Javascript in defining classes, and the jQuery library to access and manipulate the DOM.

Model
Let’s start with the implementation of a Model object. ListModel is a very simple class that implements a list of items, in which items can be added and deleted.

function ListModel() {
        this._items = null;
}
ListModel.prototype = {
        init : function (items) {
                this._items = items;
        },

        getItems : function () {
                return this._items;
        },

        addItem : function (item) {
                this._items.push(item);
        },

        removeItemAt : function (index) {
                this._items.splice(index, 1);
        }
});

View
The View implements an action form which is included inside the finditems HTML div, consisting of an input text field and a button to apply the form. The init method bind the apply button to the method findItemsController’s apply method, for the click event. The show method cleans the screen and makes the view visible, the hide method makes the view not visible, and the getInputValue method returns the value that has been inserted in the input text field.

function FindItemsView(){
        this.view = "#finditems_div";
        this.inputTextfield = "#finditems_input";
        this.applyButton = "#finditems_apply";
}
FindItemsView.prototype = {
        init : function () {
                $(this.applyButton).click(findItemsController.apply);
        },

        show : function () {
                $(this.view).show();
        },

        hide : function () {
                $(this.view).hide();
        },

        getInputValue : function () {
                return $(this.input).val();
        }
};

Controller
The FindItemsView class is tightly bound with the FindItemsControllerclass, which handles the input event from the user interface via direct calls, registered handlers or callbacks. In our example FindItemsController’s apply method is called when the apply button is pressed (this event has been register in the init method of the FindItemsView class), gets the value of the input field from the View, then performs an asyncronous call to a service: the response handler is FindItemsController itself, and three callback methods (success, error and timeout) are passed. If nothing goes wrong, the success callback is called and a new ListModel object is created and initialized with retrieved items. At this point the control is given to the ListItemsController, that has the responsibility to display the ListModel on a different View.

function FindItemsController(){
}
FindItemsController.prototype = {
        init : function () {
        },

        apply : function () {
                var input = findItemsView.getInput();
                service.findItem(input, findItemsController, success, error, timeout);
               
        },

        success : function ( items ) {
                listModel = new ListModel();
                listModel.init(items);
                listItemsController.display(listModel); /* give the control to ‘listItemsView’ */
        },

        error : function () {
                alert("Error");
        },

        timeout : function () {
                alert("Timeout");
        }
};

Alright! This is the starting point to write a MVC-based Javascript Application using pure Javascript and jQuery library. Alex Netkachov in this article shows how to write a simple MVC-based JavaScript component with the support of the Dojo language utilities. Also want to point out this interesting video about JavascriptMVC: using a framework or a toolkit is rarely a bad idea.

Well, since in this period I am extensively developing Mobile Widgets (i.e. Javascript Application), I’m going to come back on the subject soon, showing how to implement widely used design patterns, such as Observer, Facade, Singleton and Factory.

Stay tuned !

The Vodafone widget developer port-a-thon
Oct 7th, 2009 by franciov

JIL_partnersIn this moment I’m on a train heading back to Rome from the Novotel Hotel in Milan where I participated in the Vodafone widget developer port-a-thon (October 5 and 6). Vodafone and the JIL team provided a presentation of the upcoming JIL Developer Portal, market opportunity associated to mobile widgets and brand new devices. But above all this meeting has been a working session: I had to bring my laptop and to roll up my sleeves and get to work. The JIL team provided us user experience guidelines, quality criteria to follow for a widget to be accepted for publishing, showed how to port a real mobile widget and supported us all the time in developing our widgets through JIL Widget SDK and JIL Software Development Kit (Eclipse-based IDE). The JIL team also provided brand new devices on which we installed our widgets for testing purpose.

I come from mobile web site and application development for mobile handsets, and mobile widgets are rather a new thing for me despite I had to deal with Nokia Web Runtime some months ago. At first glance mobile widgets seem to incorporate all advantages associated on both classic mobile applications and browser “applications” (i.e. mobile web sites):

  • Web technology
  • Low learning curve
  • Portability
  • Easy to Layout
  • Offine content store
  • One Package

However JIL Widget SDK is actually far from perfect: lack of features (to be implemented), device heterogeneity and limitations, different Web RunTimes to be handled. Fortunately the JIL project, currently in Beta, consists of a skilled team of developers and a large community, so I’m sure platform stability will be improved, missing features will be implemented and new features will be added.

Nokia Ovi Store – Device and Application Heterogeneity
Jul 3rd, 2009 by franciov

nokia-ovi-logo

Its name is Ovi Store, has been announced at the Mobile World Congress 2009 (Barcelona) in February, and on May 26 it went live for all Nokia users in the world. More than 50 Nokia devices were compatible with the service from day one and Nokia estimated that around 50 million people with Nokia devices would be able to benefit for Ovi Store. Initial number of applications was 20.000 and new applications creation is propelled by an high percentage of money going to developers. Moreover the Nokia N97 will be the first to come out with Ovi Store pre-installed.

The Global Intelligence Alliance Group conducted an interesting analysis on  mobile market places. Different market places have been compared in terms of:

  1. time to market
  2. the ability to attract developers
  3. the rate of device adoption
  4. an efficient interface and user experience
  5. having a critical mass of attractive applications.

to get the following rankings table:

Application Marketplace iPhone Apps Store Android Marketplace Ovi Apps Store Windows Mobile Skymarket Blackberry Apps Storefront webOS Software Store
Manufacturer Apple OHA Nokia Microsoft Research In Motion Palm
Time to market 2 2 1 0 1 0
Attracting developers 2 2 2 1 2 1
Device adoption / rollout 1 0 2 2 1 0
Interface / user experience 2 2 2 1 1 2
Number, variety and appeal of apps 2 1 0 2 0 0
Summary 9 7 7 6 5 3

Source: Global Intelligence Alliance. Points are allocated as follows: 2 – strong, 1 – medium, 0 – weak based on assessment/expectations of all application stores along the parameters.

It’s interesting to note that Nokia’s rank is two points lower than iPhone Apps Store’s, but equal to Android Marketplace’s. Further information in the Global Intelligence Alliance’s article.

What distinguish Nokia Ovi Store from the other marketplaces is the device and application heterogeneity Nokia had to manage and offers. Despite iPhone Apps Store, that is clearly made for one phone, Nokia Ovi Store covers more than 50 Nokia devices, Series 40 and Series 60 included, and because of this for each mobile application the Ovi Store needs to indicate the device compatibility. Moreover, even if every application you can find inside the Ovi Store behaves and looks like it has been developed using the same technology, the same programming language, it doesn’t ! In fact, a Nokia application you download from Ovi Store might be:

  1. A Nokia Symbian Mobile Application
  2. A Java Application (MIDlet)
  3. A Nokia WRT Widget

This might be seen as an advantage for the developer because she/he has the chance to choose the technology which likes more or which better fits to application features or customer requirements. And maybe it is, we’ll see what happens in the next few months.

»  Substance: WordPress   »  Style: Ahren Ahimsa
© www.francesco.iovine.name