Anton Sarov
Spartakiade 2015
Java and Eclipse RCP developer by day
Play Framework developer by night
@duxanton
thanks to @elmanu for the slide
(Train station vs. Restaurant)
But with some limitations (due to time pressure, like in real life)
# Homepage
GET / controllers.Application.index()
# Display a person
GET /persons/:id controllers.Persons.show(id: Long)data binding
directives
backend
var app = angular.module('app', ['leaflet-directive']);
app.factory('MyService', function($rootScope) {
var mService = {};
mService.connection = {};
mService.connect = function() {
$rootScope.wsUri = "ws://localhost:9000/ws";
var ws = new WebSocket ( $rootScope.wsUri ) ;
ws.onopen = function ( ) {
mService.connection = ws;
}
ws.onmessage = function ( event ) {
$rootScope.$apply(function() {
if (JSON.parse(event.data).action!=null) {
mService.messages.push ( JSON.parse(event.data) ) ;
} else {
mService.city = JSON.parse(event.data);
mService.messages = [];
}
});
}
};
mService.postMsg = function(data) {
mService.connection.send(data);
};
mService.messages = [];
mService.city = "";
return mService;
});application.js:
app.controller('MainCtrl', function($scope, MyService, $rootScope) {
$scope.lat;
$scope.long;
$scope.player = "";
$scope.city = MyService.city;
$scope.nameSet = false;
$scope.markers = MyService.messages.map(function(msg) {
return {
lng: msg.long,
lat: msg.lat,
message: msg.player,
focus: msg
}
});
...
});application.js (controller state):
app.controller('MainCtrl', function($scope, MyService, $rootScope) {
...
$rootScope.$watch(
function() {
return MyService.city;
}, function (newValue) {
$scope.city = newValue;
$scope.lat = 0;
$scope.long = 0;
$scope.markers = [];
});
$rootScope.$watchCollection(
function() {
return MyService.messages;
}, function(newValue) {
$scope.markers = newValue.map(function(moveMessage) {
return {
lng: moveMessage.long,
lat: moveMessage.lat,
message: moveMessage.player,
focus: false
}
});
});
...
});application.js (watch functions):
app.controller('MainCtrl', function($scope, MyService, $rootScope) {
...
$scope.doSend = function() {
var data = {
action: "answer",
lat: $scope.lat,
long: $scope.long,
city: $scope.city,
player: $scope.player
};
MyService.postMsg(JSON.stringify(data));
};
$scope.join = function() {
MyService.connect();
$scope.nameSet = true;
};
...
});application.js (controller functions):
app.controller('MainCtrl', function($scope, MyService, $rootScope) {
...
$scope.$on('leafletDirectiveMap.click', function(event, args){
var latlng = args.leafletEvent.latlng;
$scope.lat = latlng.lat;
$scope.long = latlng.lng;
});
angular.extend($scope, {
defaults: {
maxZoom: 2,
minZoom: 2
},
events: {
map: {
enable: ['click'],
logic: 'emit'
}
}
});
});application.js (leaflet configuration):