Here is my flow: The main page is at '/main' url, so when the user goes to that page, my web server returns a static html:
<!DOCTYPE html>
<html ng-app="sam">
<head>
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
</head>
<body>
<div ng-controller="BuildingsController as buildings">
<button ng-click="RefreshBuildings()">Refresh</button>
<h1>{{buildings.elements.length}}</h1>
<table>
<tr>
<th>id</th>
<th>name</th>
<th>last_updated</th>
<th>based_on_file</th>
</tr>
<tr ng-repeat="element in buildings.elements | orderBy:'-id'">
<td>{{element.id}}</td>
<td><a ng-href="/getTenants?buildingId={{element.id}}">{{element.name}}</a></td>
<td>{{element.last_updated | date}}</td>
<td>{{element.based_on_file}}</td>
<td><button ng-click="buildings.UpdateDataBase(element.id)">Refresh</button></td>
</tr>
</table>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.13/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="app.js"></script>
</body>
</html>
Here is the js file:
(function(){
var app = angular.module('sam', []);
app.controller('BuildingsController', ['$http','$log', function($http, $log){
//Update scope pramaters
this.RefreshGui = function(elements){
this.elements = elements
};
//call server to get all builgings' info
this.GetBuildingsData = function(){
//'this' inside the scope of success function will point to $http, so we store the controller pointer into tmp
tmp = this;
$http.get('/fetchBuildings').success(function(data) {
//$log.log(data);
tmp.RefreshGui(data);
});
};
//call server to update building's database
this.UpdateDataBase = function(element_id){
//'this' inside the scope of success function will point to $http, so we store the controller pointer into tmp
tmp = this;
//need to serialize data in angular (http://stackoverflow.com/questions/12190166/angularjs-any-way-for-http-post-to-send-request-parameters-instead-of-json)
//as a best practice do it also when sending ajax requests via jquery
// or To globally override the default transforms, override the $httpProvider.defaults.transformRequest and $httpProvider.defaults.transformResponse properties of the $httpProvider. as suggested in http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/
$http.post('/updateBuildings', $.param({'element_id': element_id})).success(function(data) {
//$log.log(data);
tmp.RefreshGui(data);
});
};
//on launch call UpdateBuildings with no paramters, this will build the database from scratch
this.GetBuildingsData();
}]);
})();
I'm having a hard time understanding how to continue the flow when the user clicks on the link of the building name :
<td><a ng-href="/getTenants?buildingId={{element.id}}">{{element.name}}</a></td>
My intention is that the user will be presented with a new html page where he can see a table of tenants' data from the relevant building. I have no problem at the server side getting the building id and fetch those details from the database but the question is what do I return to the client? A static html ? how do I plant the dynamic details I've just fetched from the database in that html using angular? Is that a bad design in the first place? cause I think it is not consistent with angular's single page application basics.
Please help. Thanks!