-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
53 lines (43 loc) · 1.62 KB
/
app.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
var app = angular.module("dribbbleScorer", []);
app.controller("DribbbleController", function($scope, DribbblePlayer){
$scope.newPlayer = null; // Our model value is null by default
$scope.players = []; // We'll start with an empty list
// Fetches a Dribbble player and adds them to the list
$scope.addPlayer = function(player){
// We can push a new DribbblePlayer instance into the list
$scope.players.push(new DribbblePlayer(player));
$scope.newPlayer = null;
}
$scope.removePlayer = function(player){
$scope.players.splice($scope.players.indexOf(player), 1);
}
});
// We need to inject the $http service in to our factory
app.factory("DribbblePlayer",function($http){
// Define the DribbblePlayer function
var DribbblePlayer = function(player){
// Define the initialize function
this.initialize = function () {
// Fetch the player from Dribbble
var url = 'http://api.dribbble.com/players/' + player + '?callback=JSON_CALLBACK';
var playerData = $http.jsonp(url);
var self = this;
// When our $http promise resolves
// Use angular.extend to extend 'this'
// with the properties of the response
playerData.then(function(response){
angular.extend(self, response.data);
});
}
this.likeScore = function(){
return this.likes_received_count - this.likes_count;
}
this.commentScore = function(){
return this.comments_received_count - this.comments_count;
}
// Call the initialize function for every new instance
this.initialize();
}
// Return a reference to the function
return (DribbblePlayer);
});