var app = angular.module('plunker', ['ezfb', 'hljs'])
.config(function (ezfbProvider) {
/**
* Basic setup
*
* https://github.com/pc035860/angular-easyfb#configuration
*/
ezfbProvider.setInitParams({
appId: '386469651480295'
});
})
.controller('MainCtrl', function($scope, ezfb, $window, $location, $q) {
updateMe();
updateLoginStatus()
.then(updateApiCall);
/**
* Subscribe to 'auth.statusChange' event to response to login/logout
*/
ezfb.Event.subscribe('auth.statusChange', function (statusRes) {
$scope.loginStatus = statusRes;
updateMe();
updateApiCall();
});
$scope.login = function () {
/**
* Calling FB.login with required permissions specified
* https://developers.facebook.com/docs/reference/javascript/FB.login/v2.0
*/
ezfb.login(null, {scope: 'email,user_likes'});
/**
* In the case you need to use the callback
*
* ezfb.login(function (res) {
* // Executes 1
* }, {scope: 'email,user_likes'})
* .then(function (res) {
* // Executes 2
* })
*
* Note that the `res` result is shared.
* Changing the `res` in 1 will also change the one in 2
*/
};
$scope.logout = function () {
/**
* Calling FB.logout
* https://developers.facebook.com/docs/reference/javascript/FB.logout
*/
ezfb.logout();
/**
* In the case you need to use the callback
*
* ezfb.logout(function (res) {
* // Executes 1
* })
* .then(function (res) {
* // Executes 2
* })
*/
};
$scope.share = function () {
var no = 1,
callback = function (res) {
console.log('FB.ui callback execution', no++);
console.log('response:', res);
};
ezfb.ui(
{
method: 'feed',
name: 'angular-easyfb API demo',
picture: 'http://plnkr.co/img/plunker.png',
link: 'http://plnkr.co/edit/qclqht?p=preview',
description: 'angular-easyfb is an AngularJS module wrapping Facebook SDK.' +
' Facebook integration in AngularJS made easy!' +
' Please try it and feel free to give feedbacks.'
},
callback
)
.then(callback);
};
/**
* For generating better looking JSON results
*/
var autoToJSON = ['loginStatus', 'apiRes'];
angular.forEach(autoToJSON, function (varName) {
$scope.$watch(varName, function (val) {
$scope[varName + 'JSON'] = JSON.stringify(val, null, 2);
}, true);
});
/**
* Update api('/me') result
*/
function updateMe () {
ezfb.getLoginStatus()
.then(function (res) {
// res: FB.getLoginStatus response
// https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus
return ezfb.api('/me');
})
.then(function (me) {
// me: FB.api('/me') response
// https://developers.facebook.com/docs/javascript/reference/FB.api
$scope.me = me;
});
}
/**
* Update loginStatus result
*/
function updateLoginStatus () {
return ezfb.getLoginStatus()
.then(function (res) {
// res: FB.getLoginStatus response
// https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus
$scope.loginStatus = res;
});
}
/**
* Update demostration api calls result
*/
function updateApiCall () {
return $q.all([
ezfb.api('/me'),
ezfb.api('/me/likes')
])
.then(function (resList) {
// Runs after both api calls are done
// resList[0]: FB.api('/me') response
// resList[1]: FB.api('/me/likes') response
$scope.apiRes = resList;
});
}
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>angular-easyfb API demo (promise version)</title>
<link data-require="bootstrap-css@3.0.0" data-semver="3.0.0" rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="https://yandex.st/highlightjs/7.3/styles/zenburn.min.css" />
<script src="https://yandex.st/highlightjs/7.3/highlight.min.js"></script>
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.3/angular.min.js" data-semver="1.4.3"></script>
<script src="https://pc035860.github.io/angular-highlightjs/angular-highlightjs.min.js"></script>
<script src="https://pc035860.github.io/angular-easyfb/angular-easyfb.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<a href="https://github.com/pc035860/angular-easyfb"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a>
<div class="container">
<h2>angular-easyfb API demo (promise version)</h2>
<div>
<button class="btn btn-primary" ng-click="login()" ng-disabled="loginStatus.status == 'connected'">Connect with Facebook</button>
<button class="btn btn-danger" ng-click="logout()" ng-disabled="!loginStatus || loginStatus.status != 'connected'">Logout</button>
<button class="btn btn-success" ng-click="share()">FB.ui</button>
</div>
<br>
<h4>
Hello, <span ng-show="me.name">{{ me.name }}</span><span ng-show="me.error">unkown</span>!
</h4>
<div class="row">
<div class="col-md-6">
<h4 class="text-info">$FB.loginStatus()</h4>
<div class="code-block" hljs source="loginStatusJSON"></div>
</div>
<div class="col-md-6">
<h4 class="text-info">$FB.api('/me') & $FB.api('/me/likes')</h4>
<div class="code-block" hljs source="apiResJSON"></div>
</div>
</div>
</div>
</body>
</html>
/* Put your css in here */
pre {
padding: 0;
border: none;
}