Google+
Showing posts with label AngularJS. Show all posts
Showing posts with label AngularJS. Show all posts

Monday, January 5, 2015

What are Single Page Applications (SPA) for mobile and tablets

In this article we describe What are Single Page Applications (SPA) for mobile and tablets . 


What are Single Page Applications (SPA) for mobile and tablets

From years now, everything has been moving from the realm of desktop applications to the world of Web Apps. More than ever, since the apparition of mobiles and tablets. However, nowadays web applications are facing many problems , like bad user experience due to web page re-rendering, poor application performance because of page reloads and data transmission over the wire, and lack of offline support ( slow server connections imply unresponsive user interface ).

What are Single Page Applications (SPA) for mobile and tablets


Single-page applications (SPA), also called single-page interfaces (SPI), are web applications built all over a single one web page, with the goal of replicating a desktop environment in the web browser. That is achieved by means of rendering all SPA code on a single load, and calling the server ONLY when there is need of preserving data. SPAs can be run on different kinds of devices like smartphones, tablets and of course standard desktops.  SPAs use specialized frameworks like AngularJS,  Knockout and more, written in JavaScript , and they do not require page reloading .

Some examples of SPAs are Gmail,  iCloud, Google Drive, Google Maps, Hotmail, GitHub, and Soundcloud . The term "SPA" was first used by Steve Yen in 2005, and the first known experiments on Single Page Applications date from the 2002 Stuart Morris self-contained website (visit it at  slashdotslash   (works only on IE6) ) ,  and from the US patent made by Lucas Birdeau, Kevin Hakman, Michael Peachey and Evan Yeh , which describes a SPA implementation. 

SPA apps have many important features like:

1) SPA apps respond like desktop applications : this is achieved by moving data and processing from the server to the client, and by moving the business logic from server to client : this way, only data validation and permanent storage are located on server side.

2) SPA's bandwidth is minimal : the communication with the server is made in small quantities via Ajax , using the economic JSON sintax. 

3) SPA renders like desktop applications :  since there are not round-trips to retrieve HTML.

4) SPAs are accessible applications : users can use SPAs on tablets, laptops, smartphones,  etc.

5) SPAs keep working offline : a SPA can go offline if the server connection falls, and  synchronize later the browser data with the server . 

6) SPAs do not need to download updates : all that users need to do to update their apps, is to reload the browser to automatically load the latest version.

7) SPA's Navigation between "different" pages is immediate: no page reloading is necessary, since the "new" pages are generated by the SPA on the fly, without loading HTML from the server.

8) SPAs are cross-platform: of course, a SPA works on any operating system . 


SPAs are built upon the MVVM architecture , a two-way data binding software pattern,  which goal is to isolate the UI (the View) from the business logic (the Model), making data transfers using an intermediate ViewModel and Binder in the middle, to synchronize the data updates between the  View and the Model layers. The ViewModel code replaces the MVC Controller, and makes automatically all data synchronization, without any knowledge about which View is being exposed, or where is the data coming from.  This later is very useful for testing purposes.

Schematically, the MVVM architecture can be put this way:

View:
holds the user interface or presentation layer UI , with all declarative bindings

ViewModel:
stands in the middle between the View and the Model,  holds unsaved data,  and handles the application/presentation logic : as for MSDN, the ViewModel 
"decouples the View from the Model, and consolidates the business logic into the ViewModel and the Model where it can be tested"(MSDN)
If you feel a little confused on what the difference between presentation logic and business logic is, think it this way : Business logic manages data, while presentation logic manages users behaviors. 

Model:
holds data validation & business logic, and addresses the server for data read & storage:

What are Single Page Applications (SPA) for mobile and tablets 1

What are Single Page Applications (SPA) for mobile and tablets 2


In an specific Single Page Application step-by-step tutorial , we explained how to build a SPA web application in only 10 minutes , using Visual Studio 2012 templates :


What are Single Page Applications (SPA) for mobile and tablets 3




In this SPA app, the ToDo items can be added, deleted or updated.
The data is preserved on database and, after the user is done and logs out, she/he can log in again, and see all personal Todo lists as they were saved  on database on the last log out.
All the CRUD(Create Retrieve Update Delete) functionality will be hold in a SINGLE web page, while the server will be called only in case there is the need of preserving data on disk. This makes a fully functional SPA web application.


In this article we reviewed  what a Single Page Application (SPA) is. 
Happy programming.....

      by Carmel Schvartzman


כתב: כרמל שוורצמן


Wednesday, December 18, 2013

18 Step-By-Step How-to Add new rows to a Grid in AngularJS

      By Carmel Schvartzman

In this tutorial we'll learn how to Add new records to a Grid in AngularJS in our AngularJS Blogs Application. Each one of the CRUD (Create - Retrieve - Update - Delete) operations will be covered in successive walkthroughs.You can follow this step by step tutorial as a standalone, or you can learn all the posts to build the entire app. This is the STEP 18 in this walkthrough to design an end-to-end AngularJS client MVC app.

In an RESTful API the corresponding CRUD operations are done according to the HTTP request verb: the GET verb is for RETRIEVE, the POST verb is for CREATE, PUT verb  is for UPDATE and the DELETE verb for deleting of course. In previous tutorials we already used the GET and DELETE http verbs, in order to retrieve and delete data from the REST API back end. In this walkthrough we'll send requests using the POST http verb.



We'll create the Add functionality on an AngularJS table, resulting in the following Grid:



First, we'll create the REST API backend functionality, meaning the ASP.NET MVC controller method which handles the http POST requests. So go to the NatureController where our API is, and add the following method:


The method just returns a JSON object that represents the ERROR case.
Take a look at the AngularJS documentation in the AngularJS.org site: there under the $resource directive you'll find the different http verbs and its corresponding functions: we'll just use the save() function which sends POST requests to the server:



Now code an "if" to check for invalid or null input:


In case is valid, we add the post to the Blogs table, and save the context:

Remember that the Blogs entity inherits from DbSet<>, and therefore we can use its very useful methods like Remove(), Add(), Create(), find() and so on:


Remember we are using an Database First Entity Model, but we added a DbContext Code Generator :


That template created the POCO (Plain Old Clr Objects) Classes for us:


For example, this is the Blogs class automatically generated by the DbContext:


We have just upgraded our REST API made up using an MVC Controller. Now we'll go to the markup and add a button to the Grid, to create New items. Go to the BlogList.html file:


Append to the Table headers a button to go to the " #/New "  page:  in AngularJS, being a Simple Page Application, there is only one page: in a while we'll map that url to a file called "TemplateUrl" file:



Save and refresh, and take a look at the app:


Next, we'll map that address to an AngularJS Controller and a Template: find the $routeProvider in the Module at Blogs.js:


Add the following mapping, meaning that the html template in file "Details.html" must be linked to the NewCtl Controller. In the MODEL-VIEW-CONTROLLER architecture of AngularJS, a Template VIEW is related to a MODEL in the configuration step of the AngularJS app live, when we link it to some CONTROLLER.
This linking action is set in a Module, with the function of declaratively set how the AngularJS app should be bootstrapped. It's like the Main method in a program, setting the instantiation of the application.
Because AngularJS is based on the Dependency Injection paradigm, here is the place to configure the app injecting the functionality that we'll need.


Next, let's create that Controller: it will get as parameters the $scope(to get and set the scope variables), a $location (to allow us to redirect the user), and the $resource BlogResource:


As you recall from previous tutorials, the $resource was created in the Factory of our app, and it worries for the sending of requests to the web server:


Inside the Controller, we define a new function to send POST requests to the web server:

That function uses the save() function of the  $resource, sending an "$scope.blog" object to the server, named "post"(as it was named as a parameter to the Create( Blog post) MVC method in the backend):


We just use the save() function of the $resource, as explained in the AngularJS documentation:


That function defines a callback in case of success: inside we just redirect the user to the main Blogs List to see the posts, using the $location directive:



Now we create the Details.html , the html which will be the Create Form template:


Add a new html file(take care of adding it in the same folder of Index.htm and BlogList.html):


There type an H1 as a title, using the "Action" variable:


Create that variable in the Controller:


Next, add two buttons: one for Canceling and the other for Saving the posts: the Cancel just redirects the user to the main View, and the Create just calls the Create() function:


 Now because the Blog includes some properties, we'll add them to the form to edit them:


For the first property , add a div with a label and an input:


We bind the input control to the "Title" field of an "blog" object which the AngularJS engine will create for us. You can name it whatever you want, provided that the save() function sends it to the web server.

Save and refresh to see the form:


Now do the same thing for the rest of the fields to bind:


Save and refresh:


Now try to save the New post:


Nothing happens. Open the Develope r resources (F12) and go to the "Network" tab, to see the request sent. The status is 200 (OK):


Take a look at the request's Headers: the "post" object contains the data , but you can see the request's URL is targeting the "Blogs()" method:


The response received is produced by that Blogs method, not by the Create() method as we wanted:


Let's add the ActionName "Blogs" so that the request is properly handled:




Set a breakpoint inside the method to catch the request. Press the button again and see the "post" object:


It's OK, and now the response is what it shoud be:




And certainly, the post has been saved:



Finally, add styles to the Template, using the Twitter Bootstrap, and also add the ng-class directive that follows, that will  be set in case of illegal input:



Do the same for the rest of the properties:



Also add style to the buttons:



Save- Refresh to see the Simple Page Application in action. Instead of a date, input some text:



You'll see that the state is now invalid, so the post will not be saved:



Type a legal date:


This time the post has been saved:



That's all!! 
Happy programming.....


כתב: כרמל שוורצמן