Code Monkey home page Code Monkey logo

angular-easyfb's Introduction

angular-easyfb v1.4.1 Build Status

AngularJS + Facebook JavaScript SDK.

Since v1.1.0, angular-easyfb adds support for Facebook Platform versioning.

Please check out the new FB JS SDK setup doc if you want to switch platform versions (module default is v2.4).

Features

  • Full Facebook JavaScript SDK support
  • Seamless FB SDK initialization (asynchronously load script and FB.init)
  • All SDK API callbacks are automatically applied with AngularJS context
  • Support both callback and $q promise
  • Provide built-in directive support for Facebook XFBML plugins

Demos

Getting started

Include the angular-easyfb module with AngularJS script in your page.

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
<script src="http://pc035860.github.io/angular-easyfb/angular-easyfb.min.js"></script>

Add ezfb to your app module's dependency.

angular.module('myApp', ['ezfb']);

Install with Bower

bower install angular-easyfb

Usage

ezfb service

Configuration

getLocale / setLocale

Configure the locale of the original FB script file. Default locale is en_US.

angular.module('myApp')

.config(function (ezfbProvider) {
  ezfbProvider.setLocale('zh_TW');
});
getInitParams / setInitParams

Configure parameters for the original FB.init with ezfbProvider.setInitParams. (See also ezfb.init)

angular.module('myApp')

.config(function (ezfbProvider) {
  ezfbProvider.setInitParams({
    // This is my FB app id for plunker demo app
    appId: '386469651480295',

    // Module default is `v2.4`.
    // If you want to use Facebook platform `v2.3`, you'll have to add the following parameter.
    // https://developers.facebook.com/docs/javascript/reference/FB.init
    version: 'v2.3'
  });  
});
getInitFunction / setInitFunction

Customize the original FB.init function call with services injection support. The initialization parameters set in setInitParams are available via local injection ezfbInitParams.

// Default init function
var _defaultInitFunction = ['$window', 'ezfbInitParams', function ($window, ezfbInitParams) {
  // Initialize the FB JS SDK
  $window.FB.init(ezfbInitParams);
}];

Customization example:

angular.module('myApp')

.config(function (ezfbProvider) {
  var myInitFunction = function ($window, $rootScope, ezfbInitParams) {
    $window.FB.init({
      appId: '386469651480295'
    });
    // or
    // $window.FB.init(ezfbInitParams);

    $rootScope.$broadcast('FB.init');
  };

  ezfbProvider.setInitFunction(myInitFunction);
});
getLoadSDKFunction / setLoadSDKFunction

Customize Facebook JS SDK loading. The function also supports DI, with two more local injections:

  • ezfbLocale - locale name
  • ezfbAsyncInit - must called to finish the module initialization process
// Default load SDK function
var _defaultLoadSDKFunction = [
         '$window', '$document', 'ezfbAsyncInit', 'ezfbLocale',
function ($window,   $document,   ezfbAsyncInit,   ezfbLocale) {
  // Load the SDK's source Asynchronously
  (function(d){
    var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement('script'); js.id = id; js.async = true;
    js.src = "//connect.facebook.net/" + ezfbLocale + "/sdk.js";
    // js.src = "//connect.facebook.net/" + ezfbLocale + "/sdk/debug.js";  // debug
    ref.parentNode.insertBefore(js, ref);
  }($document[0]));

  $window.fbAsyncInit = ezfbAsyncInit;
}];

Customization example:

angular.module('myApp')

.config(function (ezfbProvider) {
  // Feasible config if the FB JS SDK script is already loaded
  ezfbProvider.setLoadSDKFunction(function (ezfbAsyncInit) {
    ezfbAsyncInit();
  });
});

ezfb.init

In the case that you don't want to(or you can't) configure your FB.init parameters in configuration phase, you may use ezfb.init in run phase. And any ezfb API call will not run until ezfb.init is called.

angular.module('myApp')

.run(function (ezfb) {
  ezfb.init({
    // This is my FB app id for plunker demo app
    appId: '386469651480295'
  });  
});

using ezfb

This is the original FB wrapping service, all FB.* APIs are available through ezfb.*.

No need to worry about FB script loading and Angular context applying at all.

angular.module('myApp')

/**
 * Inject into controller
 */
.controller('MainCtrl', function (ezfb) {
  /**
   * Origin: FB.getLoginStatus
   */
  ezfb.getLoginStatus(function (res) {
    $scope.loginStatus = res;

    (more || angular.noop)();
  });

  /**
   * Origin: FB.api
   */
  ezfb.api('/me', function (res) {
    $scope.apiMe = res;
  });
});

Watch the demo to see it in action.

$q promise support

Support of $q promise create more possibility for ezfb service.

Only the APIs with callback support returning promise.

Combine multiple api calls
$q.all([
  ezfb.api('/me'),
  ezfb.api('/me/likes')
])
.then(function (rsvList) {
  // result of api('/me')
  console.log(rsvList[0]);

  // result of api('/me/likes')
  console.log(rsvList[1]);
});

Watch the promise version api demo to see it in action.

Social plugins support

Facebook Social Plugins are now supported with built-in directives.

The code copied from the above link will automatically work in angular-easyfb-covered AngularJS apps.

Additionally, you can add an onrender parameter to the social plugin directive. Expressions in the onrender parameter will be evaluated every time the social plugin gets rendered.

<div class="fb-like" onrender="fbLikeRendered()"
  data-href="https://developers.facebook.com/docs/plugins/" 
  data-layout="standard" 
  data-action="like" 
  data-show-faces="true" 
  data-share="true"></div>

Demo (directives demonstration)

Demo2 (interpolated attributes)

Changelog

See the changelog here.

Develop

angular-easyfb uses Grunt to run all the development tasks.

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins.

angular-easyfb also uses Bower to manage packages for tests.

Setup

After cloning the git repo to your place, simply run following commands to install required packages.

npm install
bower install

Build

Generate a minified js file after running all the tests.

grunt

Running tests

Unit tests:

grunt test:unit

Test coverage:

grunt coverage

angular-easyfb's People

Contributors

asafdav avatar barboaz avatar cordis avatar demetriusnunes avatar lammertw avatar lukeaiken avatar pc035860 avatar shedokan avatar tx-tim avatar umurgdk avatar vnctaing avatar ystros avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.