-
Notifications
You must be signed in to change notification settings - Fork 21
/
leader-board.js
55 lines (43 loc) · 1.7 KB
/
leader-board.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var app = angular.module('leaderboard', ['firebase']);
app.constant('FIREBASE_URI', 'https://ng-fb-leaderboard.firebaseio.com/');
app.controller('MainCtrl', function (ContestantsService) {
var main = this;
main.newContestant = {lane: '', name: '', score: ''};
main.currentContestant = null;
main.contestants = ContestantsService.getContestants();
main.addContestant = function () {
ContestantsService.addContestant(angular.copy(main.newContestant));
main.newContestant = {lane: '', name: '', score: ''};
};
main.updateContestant = function (contestant) {
ContestantsService.updateContestant(contestant);
};
main.removeContestant = function (contestant) {
ContestantsService.removeContestant(contestant);
};
main.incrementScore = function () {
main.currentContestant.score = parseInt(main.currentContestant.score, 10) + 1;
main.updateContestant(main.currentContestant);
};
main.decrementScore = function () {
main.currentContestant.score = parseInt(main.currentContestant.score, 10) - 1;
main.updateContestant(main.currentContestant);
};
});
app.service('ContestantsService', function ($firebaseArray, FIREBASE_URI) {
var service = this;
var ref = new Firebase(FIREBASE_URI);
var contestants = $firebaseArray(ref);
service.getContestants = function () {
return contestants;
};
service.addContestant = function (contestant) {
contestants.$add(contestant);
};
service.updateContestant = function (contestant) {
contestants.$save(contestant);
};
service.removeContestant = function (contestant) {
contestants.$remove(contestant);
};
});