本文介绍了requireJS与fileupload插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在requirejs中,我们可以通过这个设置js的名字:

  requirejs.config({
paths :{
'jquery':'//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js'
}
});

使用它:

<$ p $ j $($'$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

但是对于像fileuploads这样的插件需要一些js文件。他们的文档显示了如何在requirejs中使用它:



但是,我怎样才能把它们全部归为一个名字呢?

  requirejs(['jquery','fileupload'],function($){
// all fileupload js已经加载并准备使用
});

所以当我需要fileupload的时候,它会加载所有需要的js而不需要一个接一个的。

谢谢。

解决方案

您可以创建一个元模块导入所有必要的资源并将它们捆绑在一起,然后结束模块成为隐式加载的传递依赖关系:


$ b

fileupload.js

  define(['jquery',
'js / jquery.iframe-transport',
'js / jquery.fileupload-ui'
],function(){
//所有需要的依赖加载
});

当您需要 fileupload 时,需要依赖关系。


In requirejs, we can set the name for js via this:

requirejs.config({
    paths: {
        'jquery': '//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js'
    }
});

And use it with this:

requirejs(['jquery'],function ($) {
    //loaded and can be used here now.
});

But for some plugin like fileuploads required a number of js files. Their document shows how to use it with requirejs: https://github.com/blueimp/jQuery-File-Upload/wiki/How-to-use-jQuery-File-Upload-with-RequireJS

But, how can I manage to put them all into one name?

requirejs(['jquery','fileupload'],function ($) {
    //all fileupload js has been loaded and ready to use
});

So that when I require fileupload, it will load all the required js without requiring them one by one.

Thank you.

解决方案

You could create a meta-module that imports all the necessary resources and bundles them together, the end modules then become transitive dependencies that are loaded implicitly:

fileupload.js

define(['jquery',
        'js/jquery.iframe-transport',
        'js/jquery.fileupload-ui'
], function () {
  // all needed dependencies loaded
});

When you require fileupload you ensure all the needed dependencies are present.

这篇关于requireJS与fileupload插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 02:23