meteormeteor-helper

FB.XFBML.parse() fails during rendering


I've been trying to execute FB.XFBML.parse(); to reload all Facebook social plugins. I can successfully execute it in Template.sample.events({}) but not in Template.sample.onRendered() or Template.sample.rendered as I am getting an error saying that FB is undefined. My code is as follows:

Template.sample.onRendered(function() {
  FB.XFBML.parse();
});

OR

Template.sample.rendered = function() {
  FB.XFBML.parse();
}

How can I execute FB.XFBML.parse(); every time a template is loaded?


Solution

  • Just wrapped the method with a try-catch block. Noticed that for rendered is being called multiple times. The first pass fails saying FB is undefined while the succeeding passes succeeds.

    Template.sample.rendered = function() {
        try {
            FB.XFBML.parse();
        } catch (e) {
            // Will normally crash but succeeding execution will be successful
        }
    }