Created
July 18, 2015 18:57
-
-
Save jbasinger/578ecd69413d452e884c to your computer and use it in GitHub Desktop.
WeatherOrNot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .config(function($stateProvider, $urlRouterProvider) { | |
| $stateProvider | |
| .state('app', { | |
| url: '/app', | |
| abstract: true, | |
| templateUrl: 'templates/menu.html' | |
| }) | |
| .state('app.weather', { | |
| url: '/weather', | |
| views: { | |
| 'menuContent': { | |
| templateUrl: 'templates/weather.html', | |
| controller: 'WeatherCtrl' | |
| } | |
| } | |
| }) | |
| .state('app.about', { | |
| url: '/about', | |
| views: { | |
| 'menuContent': { | |
| templateUrl: 'templates/about.html' | |
| } | |
| } | |
| }); | |
| // if none of the above states are matched, use this as the fallback | |
| $urlRouterProvider.otherwise('/app/weather'); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| angular.module('starter.controllers', ['openWeatherMap']) | |
| .controller('WeatherCtrl', ['$scope', '$window', 'weather', function($scope, $window, weather) { | |
| $scope.cities = []; | |
| $scope.input = {}; | |
| function onZipCodeData(zipCode, callBack){ | |
| var prom = weather.getWeatherByZipCode(zipCode); | |
| prom.then(function(data){ | |
| data.zipCode = zipCode; | |
| callBack(data); | |
| }).catch(function(msg){ | |
| $window.alert(msg); | |
| }).finally(function(){ | |
| $scope.input.zipCode = ""; | |
| }); | |
| } | |
| $scope.addZipCode = function(){ | |
| var zipCode = $scope.input.zipCode; | |
| onZipCodeData(zipCode, function(data){ | |
| $scope.cities.push(data); | |
| }); | |
| }; | |
| $scope.refresh = function(zipCode, index){ | |
| onZipCodeData(zipCode, function(data){ | |
| $scope.cities[index] = data; | |
| }); | |
| } | |
| }]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <ion-view view-title="Weather"> | |
| <ion-content> | |
| <label class="item item-input"> | |
| <input type="text" placeholder="Zip Code" ng-model="input.zipCode"> | |
| </label> | |
| <button class="button button-full button-positive" ng-click="addZipCode()"> | |
| Add Zip Code | |
| </button> | |
| <ion-list> | |
| <ion-item ng-repeat="weather in cities"> | |
| <div style="float: left;"> | |
| {{weather.name}} | |
| </div> | |
| <div style="float: right;"> | |
| {{weather.main.temp}} °F | |
| </div> | |
| <ion-option-button class="button-balanced" ng-click="refresh(weather.zipCode, $index)">Refresh</ion-option-button> | |
| </ion-item> | |
| </ion-list> | |
| </ion-content> | |
| </ion-view> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| angular.module('openWeatherMap',[]) | |
| .factory('weather', ['$http', '$q', function($http, $q){ | |
| var weather = {}; | |
| weather.getWeatherByZipCode = function(zipCode){ | |
| var defered = $q.defer(); | |
| $http.get('http://api.openweathermap.org/data/2.5/weather?zip=' + zipCode + ',us&units=imperial') | |
| .success(function(data, status, headers, config){ | |
| if(data.cod != 200){ | |
| defered.reject(data.message); | |
| } else { | |
| defered.resolve(data); | |
| } | |
| }) | |
| .error(function(data, status, headers, config){ | |
| defered.reject('Error in API'); | |
| }); | |
| return defered.promise; | |
| }; | |
| return weather; | |
| }]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment