This repository was archived by the owner on Aug 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathcommon.js
118 lines (107 loc) · 3.22 KB
/
common.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
var app = angular.module('simpleapp', ['ngRoute', 'sdr']);
var cities = [
{
code: 'boston',
displayName: 'Boston, MA',
search: 'Boston,%20MA,%20US'
},
{
code: 'new-york',
displayName: 'New York, NY',
search: 'New%20York,%20NY,%20US'
},
{
code: 'portland',
displayName: 'Portland, OR',
search: 'Portland,%20OR,%20US'
},
{
code: 'san-francisco',
displayName: 'San Francisco, CA',
search: 'San%20Francisco%20,%20CA,%20US'
},
{
code: 'seattle',
displayName: 'Seattle, WA',
search: 'Seattle,%20WA,%20US'
}
];
app.controller(
'weatherController',
function weatherController(weatherData, $scope) {
$scope.city = weatherData;
if (weatherData.main) {
weatherData.main.tempC = Math.round(weatherData.main.temp - 273.15);
}
if (weatherData.weather) {
$scope.weather = weatherData.weather[0];
}
}
);
app.controller(
'chooseCityController',
function ($scope) {
var rows = [];
var columns = [];
rows.push(columns);
angular.forEach(
cities,
function (city) {
columns.push(city);
if (columns.length == 2) {
columns = [];
rows.push(columns);
}
}
);
$scope.cityRows = rows;
}
);
var weatherTemplate = '<h2>Weather for {{weather.name}}</h2><pre>{{weather | json}}</pre>';
app.config(
function ($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider.when(
'/',
{
templateUrl: '/:static/partials/choosecity.html',
controller: 'chooseCityController'
}
);
$routeProvider.when(
'/:city',
{
templateUrl: '/:static/partials/weather.html',
resolve: {
weatherData: function ($http, $route, $q) {
var cityCode = $route.current.params.city;
var city;
angular.forEach(
cities,
function (maybeCity) {
if (maybeCity.code == cityCode) {
city = maybeCity;
}
}
);
if (! city) {
var defer = $q.defer();
defer.reject(new Error("No such city"));
return defer.promise;
}
var weatherUrl = 'http://api.openweathermap.org/data/2.5/weather?q=' + (
city.search
);
return $http.get(weatherUrl).then(function (resp) { return resp.data; });
}
},
controller: 'weatherController'
}
);
$routeProvider.otherwise(
{
template: '<a href="/">Weather</a>'
}
);
}
);