diff --git a/index.html b/index.html index d25dd93..90591f3 100644 --- a/index.html +++ b/index.html @@ -6,16 +6,30 @@ +
+
\ No newline at end of file diff --git a/jsonp-fu/jsonp-fu.js b/jsonp-fu/jsonp-fu.js index 999eb2a..df24ac2 100644 --- a/jsonp-fu/jsonp-fu.js +++ b/jsonp-fu/jsonp-fu.js @@ -174,9 +174,14 @@ var jsonpfu = {}, lib_opts[lib] = opts; script.include_lib(lib, function () { + // Library requires manual ready state (e.g. Facebook) + if (!libs[lib]) { + return; + } + if (ready_callbacks[lib]) { for (i = 0; i < ready_callbacks[lib].length; i++) { - ready_callbacks[lib].pop()(); + ready_callbacks[lib].pop().call(libs[lib]); } } }); @@ -193,8 +198,18 @@ var jsonpfu = {}, } }; + jsonpfu.manual_ready = function (lib, obj) { + libs[lib] = obj; + + if (ready_callbacks[lib]) { + for (i = 0; i < ready_callbacks[lib].length; i++) { + ready_callbacks[lib].pop().call(libs[lib]); + } + } + }; + jsonpfu.extend = function (name, lib) { - libs[name] = lib.call(this, script); + libs[name] = lib.call(this, lib_opts[name], script); }; jsonpfu.lib = function (lib) { @@ -211,7 +226,7 @@ var jsonpfu = {}, } if (libs.hasOwnProperty(lib)) { - callback.call(window); + callback.call(libs[lib]); } else { if (!ready_callbacks[lib]) { ready_callbacks[lib] = []; diff --git a/jsonp-fu/lib/facebook.js b/jsonp-fu/lib/facebook.js new file mode 100644 index 0000000..e635428 --- /dev/null +++ b/jsonp-fu/lib/facebook.js @@ -0,0 +1,67 @@ +/*global window, jsonpfu, FB */ + +/** + * Name: Facebook + * Desc: Used to query Facebook Connect. Facebook is tricky because it + * requires some extra works due to its asynchronous library loading. + * Usage: + * jfu.lib('facebook').connect_method(); + * + * Example: + * jfu.ready('facebook', function () { + * this.api('/rlefevre/feed', function (resp) { + * console.log(resp); + * }); + * }); + */ +jsonpfu.extend('facebook', function (opts, script) { + var options = { + app_id: '', + status: true, + cookie: true, + xfbml: false + }, fbroot; + + /* + * Do some special initialization when we are loaded that Facebook + * Connect needs. + */ + (function () { + var i, e; + + for (i in options) { + if (options.hasOwnProperty(i)) { + if (opts[i]) { + options[i] = opts[i]; + } + } + } + + /* + * Callback function to call when the Facebook Connect library + * is finished loading. + */ + window.fbAsyncInit = function () { + FB.init({appId: options.app_id, status: options.status, cookie: options.cookie, xfbml: options.xfbml}); + jsonpfu.manual_ready('facebook', window.FB); + }; + + fbroot = document.getElementById('fb-root'); + if (!fbroot) { + fbroot = document.createElement('div'); + fbroot.id = 'fb-root'; + document.getElementsByTagName('body')[0].appendChild(fbroot); + } + + e = document.createElement('script'); + e.async = true; + e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; + document.getElementById('fb-root').appendChild(e); + }()); + + /* + * Since we need to do a manual ready callback, we return nothing here. + */ + return; + +}); \ No newline at end of file diff --git a/jsonp-fu/lib/twitpic.js b/jsonp-fu/lib/twitpic.js index 85ac698..300ec13 100644 --- a/jsonp-fu/lib/twitpic.js +++ b/jsonp-fu/lib/twitpic.js @@ -12,7 +12,7 @@ * }); */ -jsonpfu.extend('twitpic', function (script) { +jsonpfu.extend('twitpic', function (opts, script) { // API helper methods for validation and querying var API = { validate: function (args, required) {