Introduction to SPA
Riki Pribadi
Riki Pribadi
@flakeware
Python Developer
Source Code Example:
https://github.com/rpribadi/example-spa-angular
I Hate JavaScript
So what is AngularJS?
Just another jQuery?
At some point, yes...but....
It's more than just that!
It's something bigger!
It's a framework!
SPA!
SPA? This SPA?
Oh, you mean this SPA?
So, what is SPA?
Singlepageappbook.com
"Single page apps are distinguished by their
ability to redraw any part of the UI without
requiring a server roundtrip to retrieve HTML.
This is achieved by separating the data from
the presentation of data by having a model
layer that handles data and a view layer that
reads from the models."
Why do we need SPA?
Singlepageappbook.com
"The main reason is that they allow us to offer a
more-native-app-like experience to the user."
More-Native-App-Like Experience?
Sadly speaking, yes!
Just 'more'...it's not really native.
So, IMHO...
If GMAIL (web version) is not a SPA, will it
really matter?
If you wait 2 seconds longer, will it really
matter?
So, IMHO...
It's harder than the usual web apps
It's duplicating validation step, but I guess it's
quite normal nowdays
So, IMHO...
Unless you have a damn good reason to build it
as SPA, maybe, just maybe......deep down
inside, you are a masochist developer
OR
We just love to challenge ourselves!
What does SPA look like?
Notice the URLs!
https://mail.google.com/mail/u/0/#inbox
https://mail.google.com/mail/u/0/#drafts
https://mail.google.
com/mail/u/0/#inbox/13f93d5835e1d04a
...
OK, so where should I start?
AngularJS Home Page
http://angularjs.org
That sounds nice!
And here comes the famous Hello World
example!
Followed by simple TODO app!
And suddenly they talk about modules,
directive, dependency injection, service,
factory....
I was like...
Rule of thumb
Don't be like,
"A solution tries to find a problem"
Be like,
"A problem tries to find a solution"
So what is the problem (goal)?
I want to create a SPA where:
1. I can manage my todo list
2. I can manage category of my todo list
3. Normally, I only need to see the dashboard
page where all my todo list is group by category
and I can check/uncheck the list items
OK, let's set up the project
You can manually setup your project folder
OR
You can use available project templates out
there:
- angular-seed
- ng-boilerplate
The Angular Way
I use angular-seed
Identify requirements
I will need:
- dashboard page
- CRUD page for category
- CRUD page for todo item
New Project Layout
Main Page (index.html)
<!doctype html>
<html lang="en" ng-app="myApp">
<body>
<article ng-view></article>
<footer>Let's learn AngularJS</footer>
<script src="lib/angular/angular.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/organize/category/controllers.js"></script>
<script src="js/organize/category/services.js"></script>
<script src="js/organize/todo/controllers.js"></script>
<script src="js/organize/todo/services.js"></script>
</body>
</html>
Index Category
The URL Conf: js/app.js
angular.module('myApp', ['CategoryModule.controllers']).
config(['$routeProvider', function($routeProvider) {
$routeProvider.when(
'/organize/category/index',
{
templateUrl: 'partials/organize/category/index.html',
controller: 'CategoryIndexController'
});
}]);
partials/organize/category/index.html
<h1>Category List</h1>
<table class="table">
<tr>
<th>#ID</th>
<th>Category</th>
<th>Action</th>
</tr>
<tr ng-repeat="(id, category) in categories">
<td>{{ id }}</td>
<td>{{ category.name }}</td>
<td>
<a href="#/organize/category/save/{{ id }}">Edit</a>
<a ng-click="del(id)">Delete</a>
</td>
</tr>
</table>
js/organize/category/controllers.py
angular.module('CategoryModule.controllers', ['CategoryModule.services']).
controller('CategoryIndexController', ['$scope', '$$category', function($scope,
$$category) {
$scope.categories = $$category.all();
$scope.del = function(id) {
$$category.del(id);
$scope.flash = "Category Deleted.";
};
}])
...
js/organize/category/services.js
angular.module('CategoryModule.services', []).factory('$$category', [function() {
var categories = {};
var latest_id = 1
var factory = {};
factory.all = function() {
return categories;
};
factory.del = function(id) {
if (categories.hasOwnProperty(id)) {
delete categories[id];
return true;
}
return false
};
return factory;
}]);
Why do we need Service?
It's all related to GLOBAL
Global State
Global Function
Global Helper
That's all...

Introduction to SPA with AngularJS