Facebook Like Box not loading on Ember app

坚强是说给别人听的谎言 提交于 2019-12-01 01:35:44

By default the Facebook JS SDK “goes through” your document once when it is initialized, and looks for elements to parse and convert into FB social plugins. But if you only load and add those elements later, that of course doesn’t work.

But the SDk offers a method to re-parse the document for such elements, FB.XFBML.parse – so call that after your new elements are added to the DOM. (You can either let it look through the whole document again, or pass in a reference to a specific DOM element, so that it will only evaluate a “sub-tree” of the document, for better performance.)

The problem I see is that your script is manipulating the DOM outside of Ember. This will make it impossible for ember to track the state of the DOM and manipulate it.

Your logic assumes that the script tag will be first, which you have no guarantee of since Ember is creating and removing script tags.

Rather than creating the element through code, you can simply include the script tag directly.

Put this in app/index.html:

<script src="//connect.facebook.net/en_US/sdk.js"></script>

Remove your logic from application.js and implement the following in views/about.js:

export default Ember.View.extend({

    facebook_app_id: config.APP.facebook_app_id,

    didInsertElement : function(){
        this._super();
        Ember.run.scheduleOnce('afterRender', this, function(){
              // initialize Facebook SDK
              var facebook_id = this.facebook_app_id;
              window.fbAsyncInit = function ()
              {
                FB.init({
                  appId:   facebook_id,
                  xfbml:   true,
                  version: 'v2.1'
                });
              };                    
        });
    }
});

You can also extract this functionality into a component, since it seems like a perfect candidate.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!