Facebook SDK helper class
A little helper class, that makes working with the Facebook SDK a breeze, as it takes care of including the Facebook JS SDK into the DOM, managing and keeping track of the users Facebook auth status throughout the app, allowing you to do, exactly what you want to do only quicker and easier that before.
Using the standard Facebook JS SDK you have to keep track of the user auth status, making sure that the user is logged in before making API calls to facebook, check each API response for an error and provide all the details for sharing modals. This class handles all of the above automatically.
It also keeps a list of all the API calls made before the Facebook object was ready, and only calls it once the object it ready.
Do a simple API call to get the user's basic detail, replace the content in "<â¦>" with your relevant details.
var fb_settings = {
appId: "<your_FB_app_id>",
channelUrl: <URL_to_FB_channel>,
scope: "<Facebook,scope,items,comma,separated>"
};
var fc = new FacebookConnect(fb_settings,<callback_function>)
fc.api('/me',<callback_function>);
-
isFbReady()
:Checks the status of the FB object, returns true if the FB object is ready, else false
-
api(URL,callback)
:Checks if the user is logged in with Facebook and has allowed the app access, if the user is authorised, a Facebook API call is made to the URL provided. If the user is not logged in, the default Facebook permissions dialog will appear, and once the user has authorised the app the API call will be executed.
- URL - [ string ] (required) - API URL to call.
- callback - [ function ] - The callback function.
-
getLoginStatus(callback,forceCheck)
:Gets the current login status of the Facebook user.
- callback - [ function ] - The callback function.
- forceCheck - [ boolean ] - Force reloading the login status, defaults to false.
-
login(callback)
:Attempts to log the user into Facebook.
- callback - [ function ] - The callback function.
-
logout(callback)
:Logs the user out of Facebook.
- callback - [ function ] - The callback function.
-
share(share_settings, callback)
:Display the facebook share dialog, if no share_settings was provided, it will try and populate the settings from the og:meta items in the HTML < head > section.
- share_settings - [ JSON Object, function ] Object containing redirect_uri, link, picture, name, caption, description
- callback - [ function ] - The callback function.
-
message(message_settings,callback)
: [ JSON Object, function ] Display the facebook message dialog, if no message_settings was provided, it will try and populate the link setting from the current URL.- share_settings - [ JSON Object, function ] Object containing link, to.
- callback - [ function ] - The callback function.
-
enableDebug()
:Enables debug mode, debug content will be printed out in the browser console
-
disableDebug()
:Disables debug mode.
-
getSettings()
:Returns the Facebook Settings for the app
-
getAppId()
Returns the Facebook App ID from the settings
-
setScope(scope)
:Set the Facebook scope required for the app.
- scope - [ string ] Coma separated scope list
-
getScope()
:Returns the current Facebook app scope.
-
onError(callback)
:Triggered whenever a Facebook API error occurs.
- callback - [ function ] - The callback function.
-
onAuthError(callback)
:Triggered whenever the user cancelled the Facebook auth dialog.
- callback - [ function ] - The callback function.
-
onAuthChange(callback)
:Triggered whenever the user's Facebook auth status changes.
- callback - [ function ] - The callback function.
FacebookConnect supports chaining methods to make writing your code so much easier.
var fb_settings = {
appId: "<your_FB_app_id>",
channelUrl: <URL_to_FB_channel>,
scope: "<Facebook,scope,items,comma,separated>"
};
var fc = new FacebookConnect(fb_settings,<callback_function>)
fc.onError(<callback_function>).onAuthError(<callback_function>).api('/me',<callback_function>);
MIT-License:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.