Skip to content

Commit

Permalink
Merged pull request mmangino#43 from pomartel/js-sdk.
Browse files Browse the repository at this point in the history
JS SDK initialization fix for IE
  • Loading branch information
mmangino committed Apr 27, 2011
2 parents dfcc483 + 7266fb6 commit 5969866
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 11 deletions.
20 changes: 9 additions & 11 deletions lib/facebooker2/rails/helpers/javascript.rb
Expand Up @@ -12,13 +12,15 @@ def fb_html_safe(str)
end

def fb_connect_async_js(app_id=Facebooker2.app_id,options={},&proc)
opts = Hash.new(true).merge!(options)
cookie = opts[:cookie]
status = opts[:status]
xfbml = opts[:xfbml]
opts = Hash.new.merge!(options)
cookie = opts[:cookie].nil? ? true : opts[:cookie]
status = opts[:status].nil? ? true : opts[:status]
xfbml = opts[:xfbml].nil? ? true : opts[:xfbml]
channel_url = opts[:channel_url]
lang = opts[:locale] || 'en_US'
extra_js = capture(&proc) if block_given?
js = <<-JAVASCRIPT
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
Expand All @@ -32,13 +34,9 @@ def fb_connect_async_js(app_id=Facebooker2.app_id,options={},&proc)
};
(function() {
var s = document.createElement('div');
s.setAttribute('id','fb-root');
document.documentElement.getElementsByTagName("body")[0].appendChild(s);
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
s.appendChild(e);
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol + '//connect.facebook.net/#{lang}/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
JAVASCRIPT
Expand Down
65 changes: 65 additions & 0 deletions spec/helpers/javascript_spec.rb
@@ -0,0 +1,65 @@
require "spec_helper"
describe Facebooker2::Rails::Helpers::Javascript, :type=>:helper do
include Facebooker2::Rails::Helpers
describe "fb_connect_async_js" do
it "loads with defaults" do
js = fb_connect_async_js '12345'
js.should == <<-JAVASCRIPT
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '12345',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
};
(function() {
var 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);
}());
</script>
JAVASCRIPT
end

it "disables cookies" do
js = fb_connect_async_js '12345', :cookie => false
js.include?("cookie : false").should be_true, js
end

it "disables checking login status" do
js = fb_connect_async_js '12345', :status => false
js.include?("status : false").should be_true, js
end

it "disables xfbml parsing" do
js = fb_connect_async_js '12345', :xfbml => false
js.include?("xfbml : false").should be_true, js
end

it "adds a channel url" do
js = fb_connect_async_js '12345', :channel_url => 'http://channel.url'
js.include?("channelUrl : 'http://channel.url'").should be_true, js
end

it "changes the default locale" do
js = fb_connect_async_js '12345', :locale => 'fr_FR'
js.include?("//connect.facebook.net/fr_FR/all.js").should be_true, js
end

# Can't get this to work!
# it "adds extra js" do
# helper.output_buffer = ""
# fb_connect_async_js do
# "FB.Canvas.setAutoResize();"
# end
# helper.output_buffer.include?("FB.Canvas.setAutoResize();").should be_true, helper.output_buffer
# end

end
end

0 comments on commit 5969866

Please sign in to comment.