从外部定位功能的一部分

时间:2017-05-10 12:51:51

标签: javascript jquery

第三方插件会自动将以下代码带入我的网站。

如果此区域表单中的某些必填项目为空,我想取消表单提交。我通常只使用return false,但由于此代码,这不起作用。

window.CartthrobTokenizer = {};
(function(e){
    var ender = require('ender'),
        bean = require('bean'),
        bonzo = require('bonzo'),
        qwery = require('qwery');

    e.form = null;

    e.submissionState = null;

    e.errorHandler = function(errorMessage){
        alert(errorMessage);
    };

    e.setErrorHandler = function(errorHandler){
        this.errorHandler = errorHandler;
        return this;
    };

    e.submitHandler = function(){
        CartthrobTokenizer.form.submit();
    };

    e.setSubmitHandler = function(submitHandler){
        this.submitHandler = submitHandler;
        return this;
    };

    e.bindHandler = function(){
        return true;
    };

    e.setBindHandler = function(bindHandler){
        this.bindHandler = bindHandler;
        return this;
    };

    e.beforeSubmit = function(){
        return true;
    };

    e.setBeforeSubmit = function(beforeSubmit){
        this.beforeSubmit = beforeSubmit;
        return this;
    };

    e.addHidden = function(name, value){
        bonzo(CartthrobTokenizer.form).append("<input type=\"hidden\" name=\""+name+"\" value=\""+value+"\">");
        return this;
    };

    e.val = function(selector){
        return bonzo(ender(CartthrobTokenizer.form).find(selector)).val();
    };

    e.bind = function(){
        bean.add(CartthrobTokenizer.form, "submit", CartthrobTokenizer.submit);
    };

    e.unbind = function(){
        bean.remove(CartthrobTokenizer.form, "submit", CartthrobTokenizer.submit);
    };

    e.submit = function(e){
        if (CartthrobTokenizer.submissionState === true){
            e.preventDefault();
            return false;
        }
        CartthrobTokenizer.submissionState = true;
        if (CartthrobTokenizer.beforeSubmit() !== false){
            CartthrobTokenizer.bindHandler();
        }
        e.preventDefault();
        return false;
    };

    e.init = function(bindHandler){
        this.form = qwery("#checkout_form").shift();

        this.setBindHandler(bindHandler);

        return CartthrobTokenizer.bind();
    };

})(CartthrobTokenizer);

CartthrobTokenizer.init(function(){
    Stripe.createToken({
        name: CartthrobTokenizer.val("#first_name") + " " + CartthrobTokenizer.val("#last_name"), 
        number: CartthrobTokenizer.val("#credit_card_number"),
        cvc: CartthrobTokenizer.val("#CVV2"),
        exp_month: CartthrobTokenizer.val("#expiration_month"),
        exp_year: CartthrobTokenizer.val("#expiration_year"),
        address_line1: CartthrobTokenizer.val("#address"),
        address_line2: CartthrobTokenizer.val("#address2"),
        address_city:  CartthrobTokenizer.val("#city"),
        address_state: CartthrobTokenizer.val("#state"),
        address_zip: CartthrobTokenizer.val("#zip")
        }, function(status, response){
        if (response.error) {
            CartthrobTokenizer.errorHandler(response.error.message);
            CartthrobTokenizer.submissionState = false;
        } else {
            CartthrobTokenizer.addHidden("stripeToken", response["id"])
                      .addHidden("credit_card_number", response.card["last4"])
                      .addHidden("card_type", response.card["type"])
                      .submitHandler();
        }
    })
});

以下似乎用于阻止表单在这些函数中提交:

CartthrobTokenizer.submissionState = false;

我试图使用它,但我不确定如何调用它来进入第三方的功能。

任何人都可以解释我如何定位'submitstate'功能吗?

0 个答案:

没有答案