DOJO 导入 基于AMD DOJO 配置 + 免费的CDN服务(不想用就下载DOJO使用自己的路径) 例如下
DOJO 配置介绍
- <script>
- dojoConfig= {
- has: {
- "dojo-firebug": true
- },parSEOnLoad: false,foo: "bar",async: true
- };
- </script>
- <script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.3/dojo/dojo.js"></script>
参考资料 http://dojotoolkit.org/documentation/tutorials/1.10/dojo_config/
dojoConfig 的配置文件一定要在dojo.js前面,虽然暂时还不知道这个配置里面有哪些东西,不过是基于requirejs的应该和 require.config({...}) 中有相似的部分 下面有待验证
配置项:
- baseUrl: amd module js 根路径 同requirejs baseUrl
- packages: 一个数组 提供了 包名和路径的映射
- path: AMD module 名称和路径映射 参考requirejs config
- async: 是否异步加载AMD module 可选值 true,false,legacyAsync(将 loader 永久地置为legacy cross-domain mode)
- map :Allows you to map paths in module identifiers to different paths 例
那么在引用dojo/domReady!的时候 就可以直接使用 require(['ready!']) 替代 require(['dojo/domReady!'])
- map: {
- // Instead of having to type "dojo/domReady!",we just want "ready!" instead
- "*": {
- ready: "dojo/domReady"
- }
- }
- parSEOnload: 当DOM 和所有的相关依赖都加在完毕的时候调用dojo/parser解析页面
- deps: 一个数组资源 当dojo加载的时候 需要立即加载这些资源
- callback: The callback to execute oncedepshave been retrieved
- @H_301_90@waitSeconds: 加载AMD module 超@H_301_90@@H_301_90@时设定 默认是0 永不超时
- @H_301_90@cacheBust:如果未true 可以防止 module @H_301_90@缓存 会在URL 上面添加时间戳
1. dojo 对象定义
- /**
- * 测试使用 类创建
- */
- declare("com.zsq.C1",null,{
- id:null,name:null,getName:function(){
- return this.name;
- },constructor:function(){
- // 特殊的方法 在类实例化的时候会执行该方法
- }
- });
- /**
- * 相当于JAVA的匿名内部类
- */
- var c2 = declare(null,getId:function(){
- return this.id;
- }
- });
- /**
- * 子类
- */
- var c3 = declare(c2,{
- code:null,getCode:function(){
- return this.code;
- }
- });
- /**
- * 多继承
- */
- var c4 = declare([c3,c2],{
- type:null,getType:function(){
- return this.type;
- }
- });
2. 封装为符合AMD规范的对象 // 其实也就是一个requirejs定义模块包裹一下 文件路径 my/Person.js
- define(["dojo/_base/declare"],function(declare){
- return declare(null,{
- constructor: function(name,age,residence){
- this.name = name;
- this.age = age;
- this.residence = residence;
- }
- });
- });
3. 对象的使用同样基于requirejs 通过文件路径查找相应的JS 这个和配置有关 这个这里不描述
- require(["my/Person"],function(Person){
- var folk = new Person("phiggins",42,"Tennessee");
- });
可以说是只是使用了Dojo的类定义封装
4. 静态对象 参照JAVA 其实所谓的静态就是一个全局的对象 如:java 的class 定义对象 JS 也一样 只是使用一个全局的对象来实现static
- define(['dojo/_base/declare'],function(declare){
- var Demo = declare(null,{
- constructor: function(){
- console.debug("this is Demo object #" + Demo.counter++);
- }
- });
- Demo.counter = 0;
- return Demo;
- });