Escolar Documentos
Profissional Documentos
Cultura Documentos
net developers
Introduction
We have a user story that is very simple. A user story is a software requirement written from the
point of view of an application user that follows a constrained format: "As a (user role) I want
(feature/goal) so that (I have this benefit/specific outcome)". The words between the brackets are
replaced with requirements specific to your application. Hope so a car rental management
application is a good project to build for this article. We know an application involved with two
types technologies one is server process technologies and another is client process technologies.
Suppose we know only client process technologies like:Html,Css and Javascript, except those we
don't know anything about server process technologies but we know how to use server response
data with html . Our focus is how to build a client side application with server response data.
We are going to use best JavaScript framework for purpose of data communication with server to
client and client to server that is AngularJS which helps build client side web applications.
AngularJS is an open-source project which means it can be freely used, changed and shared by
any one. AngularJS is an excellent framework for building both single page applications and line
of business applications.
There are a lot of stuff, to build in this application so I will break this article in the following
basic sections:
1. What we are going to build: Describe the purpose and the requirements of our application.
2. What we are going to use: The list of technologies for all server, front-end side
libraries and frameworks.
3. Application architecture: The entire design of our application from the lowest to the
highest level.
4. Application: Start building the core application components step by step
5. Discussion: Well discuss the choices we made in the development process and
everything that you need to know to scale up the application.
Requirement 1:
1. Car related information added to the system must be displayed.
2. For each car, relevant information and functionality must be displayed.
Pic: Car-Relevant-Information
Requirement 2:
3. There will be 2 pages related to customers. One is to view and edit them and another is for
customer list display.
Pic: Customer-List.jpg
Pic: Add-Edit-Customer.jpg
Requirement 3:
4. All cars must be displayed with their relevant information
5. When employee decides to rent a specific car to a customer through the Rent view, it should
be able.
Pic: Car-Rent-List.jpg
Pic: Employee-Rent-Car.jpg
AngularJS is a JavaScript framework that helps to build web applications. AngularJS is an opensource project which means it can be freely used, changed and shared by any one. AngularJS is
an excellent framework for building both single page applications and line of business
applications.
In the market we already have several other JavaScript frameworks that help us build to web
applications, so the benefit of using an angular is over those JavaScript framework that we
already have, dependency injection is something AngularJS does quite well, if youre new to the
concept of dependency injection dont worry well discuss in detail with examples in later
section. There is two way for data binding one of the most useful feature in AngularJS that way
data binding keeps to model and the view which sink at all times so that it means changes to the
model updates the view automatically and similarly a change to the view updates the model.
Angular application cord model-view-controller with Angular, it is very easy to develop
applications in a clean MVC way, where all of you have to do is split your application called
MVC component that is model, view, controller and connect them together is done by angular
automatically and there are many more benefits as well like controlling the behavior of Dom
elements using directives and the flexibility that angular filter provide.
In our app we will use angular directives, filters, models, controller and routes.
Bootstrap 3 as the CSS framework for creating a fluent and mobile compatible interface.
Angular-ui is hosted at http://angular-ui.github.io and is one of the better known AngularJS
companion libraries. It has several AngularJS modules, such as UI.Utils and UI Bootstrap that
can be used independently from one another. The UI.Utils module provides directives such as
jQuery Pass-through. This directive allows the use of jQuery functions or plugins directly in
AngularJS, without having to create a new directive.
Moment is as popular date utility library, I initialized the start and end dates using this library.
Application architecture
We have seen both application's requirements and the technologies that we are going to use.
Now it's time to design a decoupled and scalable solution. There are two different designs we
need to provide here. The first one has to do with the entire project's solution structure and how
is this divided in independent components, the second one has to do with the Application
structure itself that is how angularJS folders and files will be organized.
Pic: Empty-Web-Application.jgp
The main menu for our application will contain three links to the three main views: Rentals,
Customers, and Cars. This menu should be responsive and adapt to smaller screen sizes because
we used the nav-bar component and kept the recommended markup. However, if you realize the
browser to a width of under 768 px, you will notice that the menu collapses and that this does not
have any effect on the button that should display it again. The functionality required for this
resides in a Bootstrap JavaScript file called collapse.js, which does not work with AngularJS in
its current form. Furthermore, this is where the AngularUI, UI.Bootstrap module comes to the
rescue. To install it in the project, we need to run the following command in NuGet Package
Manager Console and add a reference to the scripts/ui-bootstrap-tpls-0.10.0.js location in
index.html:
Install-Package Angular.UI.Bootstrap -Version 0.10.1
Application
Now we have necessary libraries in our project, we have index.html page now to get start with
angular, there are two simple steps the first step is to add a reference to the angularJS so Im
going to drag and drop the angular.js file on my page within the hear section so we have a
reference to the AngualJS and the second step is to include ng-app attribute somewhere within
the HTML, so Im going to include that within the HTML element. Next question that comes to
mind is, what is this ng-app attribute? In angular ng-app is called a directive there are many such
directives in angular and you can find the complete list of directives in angularjs.org
ng-app directive is a starting point of angularjs application, angular framework first check for
this directives somewhere in the HTML page, if ng-app directive is found then angular
bootstraps itself and starts to manage the sections of page that has ng-app . We add ng-app in our
HTML element so that everything will be manage by angular under this element.
Now we work to create a module, controllers and register a controller with the model. A module
is a container for different parts of your application i.e. controllers, services, directives, filters
etc. Module is required because it's the main method in other types of applications for example a
.net console application that has a main method which is the entry point into the application and
it's the responsibility of the main method to wired together different parts of the application.
Route
Our main menu contain three links to the three main views: Rentals, Customers, and Car. So one
of the most distinctive features we need for our application that capable to client-side-based
navigation between different views. If the browser switches between different application URLs,
the current view should be updated seamlessly without a page request being sent from the server.
Luckily AngularJS has built-in support for this feature, which is known as routing.
Now the index.html file has a new directive, ngView, that replaces the original players markup.
This directive will render the view that matches the current route, and you can have only one in
an Angular Application. The last step is to configure the routes in the application module config
section by setting them up with the $routeProvider component, those are available in our
project App folder app.js
myAppModule.config([
'$routeProvider', function ($routeProvider) {
$routeProvider.when('/', { templateUrl: 'scripts/app/views/default.html' });
$routeProvider.when('/cars', { templateUrl: 'scripts/app/views/carsIndex.html', controller:
'carsController' });
$routeProvider.when('/cars/new', { templateUrl: 'Scripts/app/views/carsEditor.html',
controller: 'carController' });
$routeProvider.when('/cars/:carId/edit', { templateUrl:
'Scripts/app/views/carsEditor.html', controller: 'carController' });
$routeProvider.when('/customers', { templateUrl:
'scripts/app/views/customersIndex.html', controller: 'CustomersController' });
$routeProvider.when('/customers/new', { templateUrl:
'Scripts/app/views/customerEditor.html', controller: 'CustomerController' });
$routeProvider.when('/customers/:customerId/edit', { templateUrl:
'Scripts/app/views/customerEditor.html', controller: 'CustomerController' });
$routeProvider.when('/rentals', { templateUrl: 'scripts/app/views/rentalsIndex.html',
controller: 'RentalsController' });
$routeProvider.when('/rentals/new', { templateUrl: 'Scripts/app/views/rentalsEditor.html',
controller: 'RentalController' });
$routeProvider.when('/rentals/:rentalId/edit', { templateUrl:
'Scripts/app/views/rentalsEditor.html', controller: 'RentalController' });
}
]);
Create views
Now we will create the following separate views for Cars:
An index view to display all cars.
A new view to create a new car.
An edit view to edit an existing car.
After these views are implemented, we need to create similar views for customers and rentals,
those views create process are same as cars view, we implement only cars view but article
attachment project contain customers and rentals views implementation.
We had a single controller called ApplicationController in index.html file, and the views
defined in app.js were not attached to any other controller. The ngRoute module allows you to
associate a controller with a view that is suitable for the current route. I will create a
carsController controller, which will be instantiated whenever the carsIndex.html file is
rendered. The following code shows the initial definition of the carsController controller:
var myAppModule = angular.module('myApp');
myAppModule.controller('carsController', ['$scope', 'carsService',
function ($scope, carsService) {
$scope.cars = carsService.getcars();
}
]);
Now we use a table element with Bootstrap styles, which will give the rendered content striped
rows and visible margins. The rows will be built using the ngRepeat directive in HTM.
<table class="table table-bordered table-striped table-hover table-responsive">
<thead class="bg-primary">
<tr>
<th>#</th>
<th>Name</th>
<th>Type</th>
<th>Quantity</th>
<th>Daily rent price</th>
<th></th>
</tr>
</thead>
<tbody>
When we navigate to the car edit application route, the following screenshot will be displayed:
Discussion
Scaling
There are 2 main things I would like to discuss about the application that we have created. The
first one is talk about scaling the application to support more roles. I'm talking about featurelevel scaling for example suppose that you have an extra requirement that this web application
should be accessible by external users too, the Customers. What would that mean for this
application? The first thing popped up to my mind is more templates, more Web API controllers
and last but not least even more JavaScript files. Guess what, that's all true. The thing is that after
such requirement our car-rent application will not be entirely smart Application. It could, but it
would be wrong.
Let me explain what I mean by that. From the server side and the Web API framework, we have
no restrictions at all. We were clever enough to create a custom membership schema with custom
roles and then apply Basic authentication through message handlers. This means that if you want
to restrict new views only to customers, what you need to do is create a new Role named
Fig: extended-architecture.jpg
Conclusion
Thats it, we finally finished this article having described step by step how to build an
Application using AngularJS with existing service. You can download the source code for this
project we built here where you will also find instructions on how to run the car-rent application.
I hope you enjoyed this article as much I did writing it. Please take a moment and post your
review at the comments section.