☆☆☆Dojo中define和declare的结合使用
在原生的js中是不可以创建类的,没有class这个关键字,但是在dojo中,dojo自定义了一个模块叫做dojo/_base/declare
,用这个模块我们可以创建自己的类,实现面向对象编程。
单继承例子:
define(["dojo/_base/declare"],function(declare){
return declare("namespace.Person",null,{
name:null,
age:null,
constructor: function(args){
declare.safeMixin(this,args);
},
toString:function(){
return this.name+":"+this.age
}
})
})
//调用自定义的类:
require(["js/person"], function(Person) {
var p=new Person({
name:"wpx",
age:20
});
alert(p.toString())
});
多继承例子:
define(["dojo/_base/declare"], function(declare){
var VanillaSoftServe = declare(null, {
constructor: function(){
console.log("adding soft serve");
}
});
var OreoMixin = declare(null, {
constructor: function(){
console.log("mixing in oreos");
},
kind: "plain"
});
var CookieDoughMixin = declare(null, {
constructor: function(){
console.log("mixing in cookie dough");
},
chunkSize: "medium"
});
//继承VanillaSoftServe, OreoMixin, CookieDoughMixin三个类
return declare([VanillaSoftServe, OreoMixin, CookieDoughMixin],{
constructor: function(){ console.log("A blizzard with " + this.kind + " oreos and " + this.chunkSize + "-sized chunks of cookie dough." ); } });
});
通过“return declare([VanillaSoftServe, OreoMixin, CookieDoughMixin]......;”来返回我们所定义的类(widget),然后在其它的地方通过“require”或者“define”来指明变量引用该类(widget)。数组里面的对象“[VanillaSoftServe, OreoMixin, CookieDoughMixin]”是该自定义类的基类。需要强调一点,这里的declare省略了第一个变量:类的名称,即:“declare("pkg.MyClassName", [VanillaSoftServe, OreoMixin, CookieDoughMixin]......;”,如果设定这第一个变量,它会将这个字符串存储到该类的“declaredClass”变量中,同时会将"pkg.MyClassName"作为一个全局的变量用于今后方便构建该类的对象。
这段代码返回的信息为:
adding soft serve
mixing in oreos
mixing in cookie dough
A blizzard with plain oreos and medium-sized chunks of cookie dough.
以下是几种常用的写法:
1.声明一个类并将该类返回:
define(["dojo/_base/declare", "extension/bussiness/ModuleBussiness"]
, function (declare, ModuleBussiness) {
var instance = declare(null, {
//构造函数
constructor: function (data) {
this.data = data;
},
action: function () {
ModuleBussiness.LayerToc();
}
});
return instance;
});
设置该模块的名称为ModuleLayerTocCommand,则调用方式为:
require(['ModuleLayerTocCommand'], function (command) {
var command = new command(params);
command.action();
});
2.直接返回自定义类:调用方法同样是实例化后直接调用方法。
define(["dojo/_base/declare", "dojo/topic", "walk/bussiness/DrawToolBussiness", "com/events/EventManager"]
, function (declare, Topic, DrawToolBussiness, EventManager) {
return declare(null, {
//构造函数
constructor: function (geoType, distance, redraw) {
this.geoType = geoType;
this.distance = distance;
this.redraw = redraw;
},
action: function (callbackSuc) {
EventManager.removeEvent();
EventManager.deactivateMapTool();
var params = {
geoType: this.geoType,
distance: this.distance,
redraw: this.redraw
};
DrawToolBussiness.DrawBuffer(params, callbackSuc);
},
actionBuff: function (parm) {
EventManager.removeEvent();
EventManager.deactivateMapTool();
DrawToolBussiness.DrawBuffer(parm.params, parm.callbackSuc);
}
});
});
3.使用单例模式:
define(['dojo/_base/declare', 'dojo/topic', 'dojo/dom-construct', 'dojo/dom-style', 'dojo/dom', 'dojo/on', 'dojo/domReady!'],
function (declare, topic, DomConstruct, DomStyle, dom, on) {
var instance = declare(null, {
constructor: function (kwArgs) {
if (instance.Instance) {
throw new Error('only one instance can be created');
}
},
showProcess: function (container, title) {
title = title || "正在加载...";
var div = "<div id='ProcessBarDivCover' style='width:100%;height:100%;position:absolute;left:0px;top:0px;z-index:999991;background:#000;-ms-filter:alpha(opacity=15);filter:alpha(opacity=15); opacity:0.15;text-align:center;'></div>"
var processBardiv = '<div id="ProcessBarDiv" style="background:url(themes/default/images/loading_background.png); width:90px; height:85px; left:50%;top:50%;position:absolute;z-index:999992;margin:-43px 0 0 -45px">'
+ '<img src="themes/default/images/loading.gif" alt="" style=" display:block; width:40px; margin:12px auto 0 auto" />'
+ '<div style=" color:#fff; font-size:12px; text-align:center; margin-top:8px">' + title + '</div> '
+ '</div>';
//控制父级定位
if (container) {
var position = DomStyle.get(container, "position");
if (position == undefined || position == "" || position == "static") {
DomStyle.set(container, "position", "relative");
}
DomConstruct.place(div, container, "last");
DomConstruct.place(processBardiv, container, "last");
}
else {
$(document.body).append(div);
$(document.body).append(processBardiv);
}
var $this = this;
on(dom.byId("ProcessBarDivCover"), "dblclick", function () {
var tip = confirm("是否取消当前操作?");
if (tip) {
$this.closeProcess();
$this.Cancel();
}
});
},
closeProcess: function () {
DomConstruct.destroy("ProcessBarDiv");
DomConstruct.destroy("ProcessBarDivCover");
},
Cancel: function () {///取消时候调用事件 }
});
//单例
if (instance.Instance == undefined || instance.Instance == null) {
instance.Instance = new instance();
}
return instance;
});
☆☆☆Dojo中define和declare的结合使用的更多相关文章
- ORACLE中声明变量:define variable declare
在sqlplus 环境中,声明变量的关键字:define variable declare 一.define关键字(host变量) host变量的作用是一个替换作用,是主机环境与oracle进行交互的 ...
- require、module、exports dojo中的三个特殊模块标识
查看dojo源码过程中,发现这三个模块名并不以dojo.dijit.dojox开头,在dojo加载器中属于特殊模块名. require 这是一个上下文智能的加载器. 我们通过dojoConfig配置了 ...
- Oracle 变量 之 define variable declare 用法及区别
Oracle 变量 之 define variable declare 用法及区别 Table of Contents 1. 扯蛋 2. define和accept 3. variable 3.1. ...
- Dojo初探之2:设置dojoConfig详解,dojoConfig参数详解+Dojo中预置自定义AMD模块的四种方式(基于dojo1.11.2)
Dojo中想要加载自定义的AMD模块,需要先设置好这个模块对应的路径,模块的路径就是这个模块的唯一标识符. 一.dojoConfig参数设置详解 var dojoConfig = { baseUrl: ...
- dojo中的xhrPost请求(JSON)
dojo中的xhrPost请求 dojo.xhrPost({ url:"../area.action", content:{ areaCode:areaCode }, handle ...
- dojo中获取表格中某一行的某个值
dojo中经常出现对表格中的某行进行操作,如单击某行修改.删除等.那怎样获取某行的唯一标示呢? 如查询表格中的某列有个userId,并且这个是唯一的,那么可以通过它来访问这一列 具体操作代码如下: v ...
- PHP中define和defined的区别
PHP中define和defined的区别 对于初学者会混淆这两个函数 1.define用来定义一个常量,常量也是全局范围的.不用管作用域就可以在脚本的任何地方访问 常量.一个常量一旦被定义,就不能再 ...
- C++中#define用法
http://blog.sina.com.cn/s/blog_686188ef0100klku.html #define是C语言中提供的宏定义命令,其主要目的是为程序员在编程时提供一定的方便,并能在一 ...
- c/c++中define用法详解及代码示例
https://blog.csdn.net/u012611878/article/details/52534622 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog. ...
随机推荐
- [nyoj]会场安排问题-贪心
会场安排问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工 ...
- 51nod1110(xjb)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1110 题意:中文题诶- 思路:可以将在 xi 位置,权值为 w ...
- 洛谷P3763 [TJOI2017]DNA(后缀自动机)
传送门 好像用SAM写的很少诶…… 其实我一开始也没想到要用SAM的……主要是没有想到找的时候可以dfs…… 首先建一个SAM,然后跑一遍dfs,枚举一下下一位,如果相同直接继续,否则就花费一次次数来 ...
- [Algorithm]巧用多项式系数与进制的联系
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 解决resignFirstResponder或者endEditing无效的办法
当你想要收回弹出的键盘时却发现平时用的resignFirstResponder和endEditing都失去作用时,应该考虑一下当前的TextField是否为第一响应者,如果不是第一响应者的话,自然下面 ...
- .bat 文件学习
参考文章:http://www.cnblogs.com/glaivelee/archive/2009/10/07/1578737.html 重点: @echo off 关闭回显,不显示脚本中的命令 e ...
- Java程序的运行机制和JVM
1. Java语言比较特殊, 由Java编写的程序需要经过编译步骤,但这个编译步骤不会产生特定平台的机器码,而是生成一种与平台无关的字节码(也就是.class文件).这种字节码不是可执行性的,必须使用 ...
- Java怎么把一个.log文件,以text文件方式打开,显示在桌面
总要有一个开始吧 群里面有一个哥们,问这个问题,索性记录下来, quextion: Java怎么把一个.log文件,以text文件方式打开,显示在桌面 anwser: 这里注意一个问题:拼接路径的时候 ...
- python序列化模块 json&&pickle&&shelve
#序列化模块 #what #什么叫序列化--将原本的字典.列表等内容转换成一个字符串的过程叫做序列化. #why #序列化的目的 ##1.以某种存储形式使自定义对象持久化 ##2.将对象从一个地方传递 ...
- BZOJ3622(容斥+dp)
思路 "恰k个"考虑求至少k.k+1--个容斥 题面说所有数字都不同,可以将所求转化为糖比药多的组数恰为\((n+k)/2\)的方案数 \(f[i][j]\)数组我觉得更好的理解方 ...