如何将其转换为适合 .js.coffee 的 jQuery 格式
How do I turn this into a jQuery format that fits into .js.coffee
我正在尝试实施 Stripe,他们的文档中列出了以下代码:
<script>
Stripe.setPublishableKey('pk_test_key');
jQuery(function($) {
$('#new_subscription').submit(function(event) {
var $form = $(this);
// Disable the submit button to prevent repeated clicks
$form.find('input[type="submit"]').prop('disabled', true);
Stripe.card.createToken($form, stripeResponseHandler);
// Prevent the form from submitting with the default action
return false;
});
});
function stripeResponseHandler(status, response) {
var $form = $('#new_subscription');
if (response.error) {
// Show the errors on the form
$form.find('.payment-errors').text(response.error.message);
$form.find('input[type="submit"]').prop('disabled', false);
} else {
// response contains id and card, which contains additional card details
var token = response.id;
// Insert the token into the form so it gets submitted to the server
$form.append($('<input type="hidden" name="stripeToken" />').val(token));
// and submit
$form.get(0).submit();
}
};
</script>
不过,我希望能够将其移出视图,并放入 javascript 文件中。正如许多人所知,rails javascript 文件使用 coffeescript。所以我想知道的是如何将其转换成可以在 js.coffee 文件中成功 运行 的东西?
手动操作确实是个好习惯,可以更深入地了解 coffeescript 和 js 之间的区别。
我平时比较懒,就用js2coffee。
视图中可能有一些 具有 到 运行 的部分,但这些部分通常非常明显,因为它们依赖于键或其他 Rails 服务器知道,但 js 文件无法访问。
我正在尝试实施 Stripe,他们的文档中列出了以下代码:
<script>
Stripe.setPublishableKey('pk_test_key');
jQuery(function($) {
$('#new_subscription').submit(function(event) {
var $form = $(this);
// Disable the submit button to prevent repeated clicks
$form.find('input[type="submit"]').prop('disabled', true);
Stripe.card.createToken($form, stripeResponseHandler);
// Prevent the form from submitting with the default action
return false;
});
});
function stripeResponseHandler(status, response) {
var $form = $('#new_subscription');
if (response.error) {
// Show the errors on the form
$form.find('.payment-errors').text(response.error.message);
$form.find('input[type="submit"]').prop('disabled', false);
} else {
// response contains id and card, which contains additional card details
var token = response.id;
// Insert the token into the form so it gets submitted to the server
$form.append($('<input type="hidden" name="stripeToken" />').val(token));
// and submit
$form.get(0).submit();
}
};
</script>
不过,我希望能够将其移出视图,并放入 javascript 文件中。正如许多人所知,rails javascript 文件使用 coffeescript。所以我想知道的是如何将其转换成可以在 js.coffee 文件中成功 运行 的东西?
手动操作确实是个好习惯,可以更深入地了解 coffeescript 和 js 之间的区别。
我平时比较懒,就用js2coffee。
视图中可能有一些 具有 到 运行 的部分,但这些部分通常非常明显,因为它们依赖于键或其他 Rails 服务器知道,但 js 文件无法访问。