如何正确使用Plupload与RequireJS?

时间:2014-09-09 08:47:51

标签: javascript requirejs plupload

我很难找到有关如何使用RequireJS使用Pluupload(http://www.plupload.com/)的文档。

我唯一能找到的就是:http://pastebin.com/CKGNDDP1 - 但它差不多3岁了,而且自那以后发生了很多变化。

目前我的配置是:

    requirejs.config({
        paths: {
            shim: {
                plupload: {
                    exports: "plupload"
                },
            },

            paths: {
                jquery: [
                    'lib/jquery.min'
                ],

                plupload: [
                    'lib/plupload.full.min'
                ],
            }
        }
    });

在我的页面上:

define(['jquery', 'plupload'], function($) {
    /**
     * uploader = new plupload.Uploader({......
     */
});

这几乎可以,因为它会触发文件上传,但是一旦我调用.start();它打破了(如下所述:"Uncaught TypeError: Cannot read property 'open' of undefined" - after calling ".start();" - plupload)。现在我不知道这是否与我的设置或插件有关,因为它之前没有RequireJS。

我使用最新版本的plupload(v.2.1.2)。

编辑:

看起来它已经支持RequireJS了。因为我可以远离垫片,它仍然可以工作。是否有任何我应该注意的兼容性,通过删除它,还是安全的?

1 个答案:

答案 0 :(得分:1)

我无法在Plupload的代码中找到对define的调用,如果支持Plupload,那么必需将出现在代码中RequireJS。

所以垫片是必要的。

但是,您的配置不正确。您在顶级shim内有pathspaths。他们都应该在顶层:

requirejs.config({
    shim: {
        plupload: {
            exports: "plupload"
        },
    },

    paths: {
        jquery: [
            'lib/jquery.min'
        ],

        plupload: [
            'lib/plupload.full.min'
        ],
    }
});