将数组传递给requirejs shim

时间:2017-07-26 20:45:30

标签: javascript requirejs magento2

我正在尝试使用常量联系人脚本来处理magento 2 requirejs。 (我尝试将其添加到内联但它会发生冲突并导致错误)

他们的脚本需要一个数组localizedErrMapjQuery对象。

我创建了localizedErrMap.js

define([],
    function(){
        var localizedErrMap = {};
        localizedErrMap['required'] =       'This field is required.';
        localizedErrMap['ca'] =             'An unexpected error occurred while attempting to send email.';
        localizedErrMap['email'] =          'Please enter your email address in name@email.com format.';
        localizedErrMap['birthday'] =       'Please enter birthday in MM/DD format.';
        localizedErrMap['anniversary'] =    'Please enter anniversary in MM/DD/YYYY format.';
        localizedErrMap['custom_date'] =    'Please enter this date in MM/DD/YYYY format.';
        localizedErrMap['list'] =           'Please select at least one email list.';
        localizedErrMap['generic'] =        'This field is invalid.';
        localizedErrMap['shared'] =         'Sorry, we could not complete your sign-up. Please contact us to resolve this.';
        localizedErrMap['state_mismatch'] = 'Mismatched State/Province and Country.';
        localizedErrMap['state_province'] = 'Select a state/province';
        localizedErrMap['selectcountry'] =  'Select a country';

        return localizedErrMap;
    }
);

并制作了此requirejs-config.js

var config = {
    "shim": {
        "constantcontact": {
            "deps":["jquery", "localizedErrMap"],
            "depnames":["jQuery", "localizedErrMap"]
        }
    },
    "paths": {
        "localizedErrMap": "js/localizedErrMap",
        "constantcontact": "https://static.ctctcdn.com/h/contacts-embedded-signup-assets/1.0.2/js/signup-form"
    }

};

然后在页面中使用这样的常量联系人形式调用它:

<script>
   require(["constantcontact"]);
</script>

他们的脚本会抛出错误:

  

未捕获的ReferenceError:未定义localizedErrMap

我无法修改他们的代码,因为它不在现场,我需要确保将此数组传递给代码。我做错了什么以及如何确保数组传递?

2 个答案:

答案 0 :(得分:1)

您在depnames中使用的shim数组不是RequireJS支持的数组。我能找到的唯一参考是在一个问题报告中,有人suggested支持这样的选项,但从未实现过。

看起来constantcontact,它不是AMD模块,期望在全局空间中找到符号localizedErrMap。一般来说,您有几个选择:

  1. 您可以为构建应用程序添加构建步骤,以便在define中围绕代码添加constantcontact调用,从而有效地将其转换为AMD模块。 r.js的{​​{3}}选项对此非常有用。但是,你说你不能修改它,所以这个选项不在桌面上。

  2. 您可以故意泄漏localizedErrMap。这种泄漏有时是使用非AMD库的库的可行解决方案。我宁愿避免它,但有些情况是无法避免的。

答案 1 :(得分:0)

我通过将其添加到内联的CMS页面来实现它。

        <script type="text/javascript">
            var localizedErrMap = {};
            localizedErrMap['required'] =       'This field is required.';
            localizedErrMap['ca'] =             'An unexpected error occurred while attempting to send email.';
            localizedErrMap['email'] =          'Please enter your email address in name@email.com format.';
            localizedErrMap['birthday'] =       'Please enter birthday in MM/DD format.';
            localizedErrMap['anniversary'] =    'Please enter anniversary in MM/DD/YYYY format.';
            localizedErrMap['custom_date'] =    'Please enter this date in MM/DD/YYYY format.';
            localizedErrMap['list'] =           'Please select at least one email list.';
            localizedErrMap['generic'] =        'This field is invalid.';
            localizedErrMap['shared'] =         'Sorry, we could not complete your sign-up. Please contact us to resolve this.';
            localizedErrMap['state_mismatch'] = 'Mismatched State/Province and Country.';
            localizedErrMap['state_province'] = 'Select a state/province';
            localizedErrMap['selectcountry'] =  'Select a country';
            var postURL = 'https://visitor2.constantcontact.com/api/signup';
        </script>
        <script>
            require(["jquery"], function($){
                $.getScript('https://static.ctctcdn.com/h/contacts-embedded-signup-assets/1.0.2/js/signup-form.js');
            });
        </script>