How to include legacy module in Dojo AMD

天大地大妈咪最大 提交于 2019-12-24 15:24:56

问题


I'm trying to migrate an application from dojo 1.6 to version 1.9.1, and I've a legacy module that I didn't want to migrate yet (it's pretty complex and will take me some time to understand). The Dojo docs indicate you can load legacy modules along with AMD modules, but when I try, I'm getting a "dojo.provide is not a function" when the loader tries to load the legacy module.

My script:

require([..., "agsjs/dijit/TOC","dojo/domReady!"], 
            function(..., TOC) {
    on(map,'layers-add-result',function(results){
        //Add Legend
        var toc = new TOC({
            map: map,
            layerInfos:legendLayers
        }, 'legendDiv');
        toc.startup();
    });
});

The first line of code of the module:

dojo.provide('agsjs.dijit.TOC');

Everything works until the loader tries to load the agsjs/dijit/TOC module, where I get a "dojo.provide is not a function" error. How do I solve this without having to refactor the entire module to AMD? Thanks


回答1:


In order for legacy modules to load, you need to run the loader in legacy mode, which means you cannot set async: true. As long as you are running with async: false (the default), you will be able to load and use legacy modules from AMD modules, and vice-versa.




回答2:


A good point of AMD is that you don't have to use "dojo" and "dijit" global variables now. If you don't want change all those dojo.xxx calls in your old modules, you may try to wrap you old module in a

define([
    "dojo/_base/declare", 
    "dojo", "dijit", 
        ...  
], function(declare, dojo, dijit) {

   return declare([/*your parent widgets*/], {

   //your old module content at here, maybe  you need make little modifications of your old module

   });

});  

So that those dojo.xxx functions may still works.

This link may provide everything you need:

http://dojotoolkit.org/reference-guide/1.9/releasenotes/migration-17.html



来源:https://stackoverflow.com/questions/19054350/how-to-include-legacy-module-in-dojo-amd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!