What is the best way to add an external javascript file / external API

Hello, I am trying to add a library called Conekta (https://www.conekta.io/en/docs/references/conekta-js) into my Meteor app after searching I found out this way:

Template._agregarTarjeta.rendered= function(){
	console.log('Estás aquí');
	$('body').append('<script type="text/javascript" src="https://conektaapi.s3.amazonaws.com/v0.3.2/js/conekta.js"></script>');
	Conekta.setPublishableKey("key_MTfwBQoC6bgzKybEHk9hLWg");
}

But it didn’t work, so I would like to know what is the best wat to add this library/API to my app, so that I can use the methods it has, thanks in advance!

3 Likes

I don’t know is it the right way but how about https://github.com/meteorhacks/meteor-inject-initial/
or just put the file in the compatibility folder if it isn’t ready to work with meteor.

1 Like

Thanks for the reply, but I think that this is to add to a pacjage right? I would just like to use the methods from the external file on Template._agregarTarjeta.rendered and later on Template._agregarTarjeta.events

Hey @cosio55, take a look at https://github.com/MiroHibler/meteor-preloader :wink:

3 Likes

Have you tried to use $.getScript in Meteor.startup on the client? Meteor.startup runs when the DOM is ready. You then check in your Template rendered for conectaReady

Session.set('connectaReady', false);
Meteor.startup(function(){
 $.getScript('https://conektaapi.s3.amazonaws.com/v0.3.2/js/conekta.js', function(){
  // script has loaded
  Session.set('conectaReady', true);
 });
});
11 Likes

Thanks everyone for the replys! At last I found Jamgold’s reply to be the easiest way to implement this. :wink: