Facebook Like Box not loading on Ember app

前端 未结 2 1367
南旧
南旧 2021-01-07 13:58

I\'m trying to get the like box to work inside our ember app, in a template called about. The problem is that if I enter the ember app from another route (inste

相关标签:
2条回答
  • 2021-01-07 14:51

    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.)

    0 讨论(0)
  • 2021-01-07 14:55

    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.

    0 讨论(0)
提交回复
热议问题