Last active
August 25, 2016 06:03
-
-
Save ashteya/a8b5de5d47ee069e2e83 to your computer and use it in GitHub Desktop.
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.4/angular.min.js"></script> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> | |
<script src="main.js"></script> | |
</head> | |
<body ng-app="myApp" ng-controller="myController"> | |
<div> | |
<input type="text" ng-model="username" /> | |
<input type="password" ng-model="password" /> | |
<button ng-click="doLogin()">Login</button> | |
</div> | |
<div> | |
<ul> | |
<li ng-repeat="dinner in dinners">{{ dinner.Name }} at {{ dinner.Date }}</li> | |
</ul> | |
</div> | |
</body> | |
</html> |
This file contains 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
(function () { | |
var dinnerService = function ($http) { | |
return { | |
login: function (username, password) { | |
var request = { | |
method: 'POST', | |
url: 'http://www.nerddinner.com/Account/LogOn', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded' | |
}, | |
data: 'UserName=' + username + '&Password=' + password + '&RememberMe=true' | |
}; | |
return $http(request); | |
}, | |
getMyDinners: function () { | |
var parseDinners = function (response) { | |
var tmp = document.implementation.createHTMLDocument(); | |
tmp.body.innerHTML = response.data; | |
var items = $(tmp.body.children).find('.upcomingdinners li'); | |
var dinners = []; | |
for (var i = 0; i < items.length; i++) { | |
var dinner = { | |
Name: $(items[i]).children('a')[0].innerText, | |
Date: $(items[i]).children('strong')[0].innerText | |
}; | |
dinners.push(dinner); | |
} | |
return dinners; | |
} | |
return $http.get('http://www.nerddinner.com/Dinners/My') | |
.then(parseDinners); | |
} | |
} | |
}; | |
var myController = function ($scope, $http, dinnerService) { | |
$scope.doLogin = function () { | |
var onSuccess = function (response) { | |
console.log("Login OK!"); | |
dinnerService.getMyDinners().then( | |
function (response) { | |
$scope.dinners = response; | |
}); | |
}; | |
dinnerService.login($scope.username, $scope.password) | |
.then(onSuccess); | |
} | |
}; | |
var myApp = angular.module('myApp', []); | |
myApp.config(function ($httpProvider) { | |
$httpProvider.defaults.withCredentials = true; | |
}); | |
myApp.factory('dinnerService', ['$http', dinnerService]); | |
myApp.controller('myController', ['$scope', '$http', 'dinnerService', myController]); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment