How can I use a Javascript library (downloaded from a CDN) inside JSNI code?
For example, I would like to call the javascript Stripe method, from within this JSNI method:
private native void contactStripe(String creditCard, String cvc, String expiryMonth, String expiryYear) /*-{
$wnd.Stripe.setPublishableKey('my_stripe_publishable_key');
$wnd.Stripe.createToken({
number: creditCard,
cvc: cvc,
exp_month: expiryMonth,
exp_year: expiryYear
}, callBack);
}-*/;
... but the Stripe javascript method is undefined.
(more on the Stripe.createToken method https://stripe.com/docs/tutorials/forms#create-a-single-use-token)
The Stripe javascript file is injected using the CDN url:
ScriptInjector.fromUrl("https://js.stripe.com/v1/").setCallback(
new Callback<Void, Exception>() {
public void onFailure(Exception reason) {
}
public void onSuccess(Void result) {
contactStripe("0000111122223333", "456", "04", "2014");
}
}).inject();
If you want the JS script to be injected in a way that the global variables it defines are accessible through $wnd
, you have to setWindow
(
ScriptInjector.TOP_WINDOW
)
.