简体   繁体   English

一般的Dojo模块加载

[英]Dojo module loading in general

Coming from Java/C# I struggle to understand what that actually means for me as a developer (I'm thinking too much object-oriented). 来自Java / C#,我很难理解这对我作为开发人员实际上意味着什么(我在想太多面向对象的问题)。

Suppose there are two html files that use the same Dojo module via require() in a script tag like so: 假设有两个html文件通过脚本标签中的require()使用相同的Dojo模块,如下所示:

<script type="text/javascript">
require(["some/module"],    
   function(someModule) {    
      ...
   }            
);
</script>

I understand that require() loads all the given modules before calling the callback method. 我知道require()在调用回调方法之前会加载所有给定的模块。 But, is the module loaded for each and every require() where it is defined? 但是,是否为定义它的每个require()加载了模块? So in the sample above is some/module loaded once and then shared between the two HTMLs or is it loaded twice (ie loaded for every require where it is listed in the requirements list)? 因此,在上面的示例中,some / module加载了一次,然后在两个HTML之间共享,还是加载了两次(即,在需求列表中列出的每个需求都被加载)?

If the module is loaded only once, can I share information between the two callbacks then? 如果模块仅加载一次,那么我可以在两个回调之间共享信息吗? In case it is loaded twice, how can I share information between those two callbacks then? 如果它被加载两次,那我该如何在这两个回调之间共享信息?

The official documentation says "The global function define allows you to register a module with the loader. ". 官方文档说:“全局函数定义允许您向装载程序注册模块。”。 Does that mean that defines are something like static classes/methods? 这是否意味着定义类似于静态类/方法?

If you load the module twice in the same window, it will only load the module once and return the same object when you request it a second time. 如果在同一窗口中两次加载模块,则它将仅加载一次模块,并在第二次请求时返回相同的对象。

So, if you're having two seperate pages, then it will have two windows which will mean that it will load the module two times. 因此,如果您有两个单独的页面,则它将有两个窗口,这意味着它将两次加载模块。 If you want to share information, you will have to store it somewhere (the web is stateless), you could use a back-end service + database, or you could use the HTML5 localStorage API or the IndexedDB (for example). 如果要共享信息,则必须将其存储在某个地方(网络是无状态的),可以使用后端服务+数据库,也可以使用HTML5 localStorage API或IndexedDB(例如)。

If you don't want that, you can always use single page applications. 如果您不希望这样做,则始终可以使用单页应用程序。 This means that you will load multiple pages in one window using JavaScript (asynchronous pages). 这意味着您将使用JavaScript在一个窗口中加载多个页面(异步页面)。


About your last question... with define() you define modules. 关于最后一个问题...使用define()定义模块。 A module can be a simple object (which would be similar to static classes since you don't have to instantiate), but a module can also be a prototype, which means you will be able to create instances, for example: 模块可以是一个简单的对象(由于不必实例化,因此它与静态类相似),但是模块也可以是原型,这意味着您将能够创建实例,例如:

define([], function() {
  return {
    "foo": function() {
      console.log("bar");
    }
  };
});

This will return the same single object every time you need it. 每次需要时,它将返回相同的单个对象。 You can see it as a static class or a singleton. 您可以将其视为静态类或单例。 If you require it twice, then it will return the same object. 如果需要两次,它将返回相同的对象。

However, you could also write something like this: 但是,您也可以这样写:

define([], function() {
  return function() {
    this.foo = function() {
      console.log("bar");
    };
  };
});

Which means that you're returning a prototype . 这意味着您将返回原型 Using it requires you to instantiate it, for example: 使用它需要实例化它,例如:

require(["my/Module"], function(Module) {
  new Module().foo();
});

Prototyping is a basic feature of JavaScript, but in Dojo there's a module that does that for you, called dojo/_base/declare . 原型制作是JavaScript的基本功能,但是在Dojo中,有一个为您执行此操作的模块,称为dojo/_base/declare You will often see things like this: 您经常会看到类似这样的信息:

define(["dojo/_base/declare"], function(declare) {
  return declare([], {
    foo: function() {
      console.log("bar");
    }
  });
});

In this case, you will have to load it similarly to a prototype (using the new keyword). 在这种情况下,您将必须类似于原型加载它(使用new关键字)。

You can find a demo of all this on Plunker . 您可以在Plunker上找到所有这些的演示。


You might ask, how can you tell the difference between a singleton/static class module, and a prototypal module... well, there's a common naming convention to it. 您可能会问,如何区分单例/静态类模块和原型模块之间的区别……嗯,有一个通用的命名约定。 When your module starts with a capital letter (for example dijit/layout/ContentPane , dojo/dnd/Moveable , ...) then it usually means the module requires instances. 当您的模块以大写字母开头(例如dijit/layout/ContentPanedojo/dnd/Moveable等)时,通常意味着该模块需要实例。 When the module starts with a lowercase letter, it's a static class/singleton (for example dojo/_base/declare , dijit/registry ) 当模块以小写字母开头时,它是静态类/单例(例如dojo/_base/declaredijit/registry

1) dojo require, loads the module once and then if you called it again require() will simply return if the package is already loaded. 1)dojo require,加载一次模块,然后再次调用它, require() will simply return if the package is already loaded. so the request will be called once and it will also call any dependencies once. 因此该请求将被调用一次,并且还将调用所有依赖项一次。

but all that if you are in the same HTML page if you leave the page and call the same module in a different page then it will be called from the server. 但是如果您在同一个HTML页面中,而又离开页面并在另一个页面中调用同一模块,那么所有这些都将从服务器中调用。 you can also use cache in your config settings so things will be cached in the browser and the file will or not by setting the cacheBust to true if you want a fresh copy or false if you want things to be cached. 您还可以在配置设置中使用缓存,这样就可以将内容缓存在浏览器中,是否将文件缓存在文件中,方法是将cacheBust设置为true(如果要重新复制),或者将false设置为false(如果要缓存)。

2) if you are in the same html page and domain, the module didn't change the module will be the same and you can share values and any change you make you can get it from anywhere unless you call a new instance. 2)如果您位于相同的html页面和域中,则未更改的模块将相同,并且您可以共享值,并且所做的任何更改都可以从任何地方获取,除非调用新实例。 but that is not possible between different html pages. 但这在不同的html页面之间是不可能的。

3) not it is not like a static classes or methods from what I understand static methods A static class can be used as a convenient container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields. 3)从我理解的静态方法来看,它不是一个静态类或方法A static class can be used as a convenient container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields. . dojo work differently it is a reference for an object if you did it in that way : 如果您以这种方式进行操作,那么dojo的工作方式将有所不同:

define(function(){
    var privateValue = 0;
    return {
        increment: function(){
            privateValue++;
        },

        decrement: function(){
            privateValue--;
        },

        getValue: function(){
            return privateValue;
        }
    };
});

This means every bit of code loads that module will reference the same object in memory so the value will be the same through out the use of that module. 这意味着模块加载的每一位代码都将引用内存中的同一对象,因此在使用该模块时其值将相同。

of course that is my understanding please feel free to tell me where I am wrong. 当然,这是我的理解,请随时告诉我我哪里错了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM