如何从我使用require.js的文件中调用对象

时间:2018-06-28 13:19:47

标签: requirejs

我在HTML页面上调用对象AjaxForm

<script>
        require(['app', '/assets/build/js/app/ajaxform.js'], function (a) {
            a.AjaxForm.initialize({$_modx->getPlaceholder('AjaxForm.params')});
        });
</script>

但是我得到了错误

Cannot read property 'AjaxForm' of undefined

我的ajaxform.js文件

define('app', ['jquery'], function ($) {
    'use strict';
    var AjaxForm = {
        initialize: function (afConfig) {

            $(document).ready(function () {
                $.jGrowl.defaults.closerTemplate = '<div>[ ' + afConfig['closeMessage'] + ' ]</div>';
            });

.......

2 个答案:

答案 0 :(得分:0)

您要返回AjaxForm吗?我认为您缺少此功能:

define('app', ['jquery'], function ($) {
    'use strict';
    var AjaxForm = {
        initialize: function (afConfig) {

            $(document).ready(function () {
                $.jGrowl.defaults.closerTemplate = '<div>[ ' + afConfig['closeMessage'] + ' ]</div>';
            });
        }
    }

    return AjaxForm;
});

然后,在您的require中,执行以下操作:

<script>
    require(['app', '/assets/build/js/app/ajaxform.js'], function (AjaxForm) {
        AjaxForm.initialize({$_modx->getPlaceholder('AjaxForm.params')});
    });
</script>   

希望有帮助

答案 1 :(得分:0)

我这样做了:

requirejs(["app", "app/ajaxform"], function () {
    require(['ajaxform'], function (e) {
        e.initialize({$_modx->getPlaceholder('AjaxForm.params')});
    });
});

在文件ajaxform.js中

define('ajaxform', ['jquery','jgrowl','jqueryform'], function ($,jGrowl,ajaxSubmit) {
....
return AjaxForm;