【问题标题】:How to avoid multiple instances of braintree.setup in Angular?如何避免 Angular 中的多个braintree.setup 实例?
【发布时间】:2016-01-05 18:15:13
【问题描述】:

我在模式窗口中有一个简单的 Braintree 付款表单:

$scope.displayModalBraintree = function () {
    $scope.modal = 'modal_payment_form.html', $scope.$on('$includeContentLoaded', function () {
        braintree.setup('tokenStringFromServer', 'paypal', {
            container: 'paypal',
            locale: 'da_dk',
            onReady: function (integration) {
                console.log('ready', integration)
            }
        })
    })
})

单击按钮执行 displayModalBraintree,第一次一切正常。但是,第二次单击按钮会生成两个 Paypal 按钮,并且日志显示准备就绪。

我已尝试按照https://github.com/braintree/braintree-web/issues/29#issuecomment-137555915 中的说明使用拆卸来销毁braintree.setup 实例,尽管它没有任何区别。

【问题讨论】:

    标签: javascript angularjs paypal braintree


    【解决方案1】:

    完全披露:我在 Braintree 工作。如果您还有任何问题,请随时contact support

    每次调用$scope.displayModalBraintree 时,您都会为$includeContentLoaded 设置一个新的侦听器。由于这些侦听器没有得到清理,因此您每次都创建一个新侦听器。所以第二次运行$scope.displayModalBraintree,它调用braintree.setup 两次。第三次运行它,因为现在有三个监听器,所以它运行了 3 次。

    一种解决方案是将侦听器设置在$scope.displayModalBraintree 之外,如下所示:

    var clientToken, braintreeVault;
    
    $scope.$on('$includeContentLoaded', function () {
      braintree.setup(clientToken, 'paypal', {
        container: 'paypal',
        onReady: function (integration) {
            braintreeVault = integration;
        }
      })
    });
    
    $scope.displayModalBraintree = function () {
      $scope.getToken().then(function (token) {
        clientToken = token;
        $scope.modal = 'modal_payment_form?' + (new Date().getTime());
      })
    };
    

    另一种选择是在加载模式后销毁侦听器,但我认为将其从$scope.displayModalBraintree 函数中提取出来更有意义。

    【讨论】:

      【解决方案2】:

      我为 Braintree 工作。

      自从发布 Github 评论后,我们更新了有关拆解的文档。尝试在您的拆卸函数中设置integration = null,如code snippet here 中所示。如果这不起作用,我建议发布调用 teardown 的代码以帮助我们诊断您的问题。

      【讨论】:

      • 谢谢,integration = null 没有帮助,我在jsfiddle.net/7h242u3q/1 发布了一个无效的示例第一次显示模态窗口,日志输出:ready Object { teardown=function()} teardown Object { teardown=function()} nulled null 第二次显示模态窗口,日志输出:ready Object { teardown=function()} ready Object { teardown=function()} teardown Object { teardown=function()} nulled null
      • 我需要一个让商家添加多个付款详细信息的工作流程。但是,当我通过提供商家 ID 为客户调用 client_token 时,我得到的只是之前添加的付款方式,没有输入表格新细节
      猜你喜欢
      • 2020-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多