Http://dojotoolkit.org/documentation/tutorials/1.10/cdn/index.html
It is useful to load dojo modules from a CDN. Loading dojo from a CDN and loading the local modules at the same time may seem impossible, but in this tutorial we will learn how to achieve this.
Introduced
Once upon a while, it was useful to load a dojo module from a CDN. For example, create a simple test scenario that you can run at any time, or provide code that is easy to distribute and run. But with dojo loaded directly from the CDN, the local custom model based on the path configuration is not easy to load. To enable CDN and local custom models to run together, you need to set up some configuration information on dojo.
By comparison, it was found that the performance of using a CDN was worse than using a locally hosted Dojo library. In particular, the use of local hosting can significantly reduce the number of HTTP requests. If you're using a CDN to improve performance, you need to weigh it carefully.
Load our Modules
Using a CDN to load the Dojo module, create a simple page with the following code:
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <title>Demo</title>5 </Head>6 <Body>7 <ScriptData-dojo-config= "Async:1"8 src= "//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></Script>9 </Body>Ten </HTML>
This code sets up the AMD (asynchronous module definition) mechanism to open and can load the Dojo module.
Before the 1.7 release, if you want to cross the domain, you need to load dojo.xd.js. Because AMD's original ecosystem supports cross-domain loading, this is not needed. Note In the script URL address there is no http:;this means that the same protocol would be used to load from the CDN as was used for the current page (i . E. If the current page loads-over-HTTPS, so would the code from the CDN).
Next we want to verify that dojo can access the local dojo/resources/blank.html
files, and we use the Load module method to test if we can access the module. The code is as follows.
1 <script data-dojo-config= "async:1, Dojoblankhtmlurl: '/path/to/blank.html '"2 src= "// Ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js "></script>
Below we need to define the path of the local package.
1 <script data-dojo-config= "async:1, Dojoblankhtmlurl: '/blank.html ',2 Packages: [{3 name: ' Custom ',4location : Location.pathname.replace (/\/[^/]+$/, ') + '/js/custom '5 }] "6 src="// Ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js "></script>
Note that the local package is the using-a little JavaScript trickery to create a absolute path, which is derived from The path of the current HTML file.
Note that we have used a small piece of JavaScript's deceptive code to create a relative path that can be connected to an HTML file. In dojo1.10, a very correct selection is used when loading modules with relative paths. This dojoBlankHtmlUrl
also applies on the. How we can, we can fix the local package and load the module. We can load our own defined modules just like normal modules.
1 function (Thinger) { ... });
Precautions
Unlike the old Dojo loader, nothing different needs to being done when using built modules from CDN. However, there is a issue that the may run into when using Dojo loaded from CDN:
- Attempting to load unbuilt, remote AMD modules, the
dojo/text
plugin would fail due to Cross-origin security R Estrictions. (Built versions of AMD modules is unaffected because the calls to is eliminated by the dojo/text
build system.)
Unlike the previous Dojo loader, there is no difference between using a dojo library on a CDN and using a local library. Then, in general, when you raise the CDN with Dojo, you will encounter some common problems.
Conclusion
cdn-based versions of Dojo can is useful in some circumstances. By making a few simple configuration changes, it's possible to use custom local modules while loading Dojo from a CDN, th Anks to the new amd-based module system.
Link
- Dojo Configuration Reference Guide
- More information to the library CDNs that Dojo Uses:google CDN and Yandex CDN.
CDN (translator)