前言:

dojo1.10已经有了原生的ConfirmDialog

做gui应用开发,肯定要用到"确认"对话框, 无论是winForm, swing,还是web,也不管理你用什么技术. 在web开发中很多成熟的开发套件中肯定会有"确认对话框"这个组件, 但是如果你用dojo的话, 那就有点可惜了, 因为它没有, 如果因为这个事你很生气, 那是可以理解的, 因为大家都很生气, 所有在dojo官网的FAQ中,就有人问:

Dojo FAQ: Does Dijit have a confirm dialog?

当然, dojo的作者们有自己的理由:

No, Dijit does not provide pre-configured dialogs, like alert, confirm, or prompt.

Why? As simple as these may seem on the surface, each organization or application often needs something subtly different in terms of layout, appearance, behavior, events, and internationalization. Dojo and Dijit provide all the elements you need to make a variety of dialogs, including dijit/Dialogdijit/form/Buttondojo/ondojo/Deferred anddojo/i18n.

他们的解释的大概意思就是: 你们要的东西自己构建起来很简单的, 我们都给你们开发了这么多组件, 自己拼拼不会啊, 你们真是伸手党. 况且, 这个玩艺也没法做公用的啊, 每个公司都有自己的需求和界面.

我只能说,你妹啊, 我就想要个基本功能的就行了, 而且我是个新手菜鸟啊, 还得想怎么样开发一个. 牢骚发完了, 事还得做, 嗯 . 开始吧.

代码:

我要的东西很简单, 如下:

就是一个弹出确认框, 有两个按钮.

define([
"dojo/_base/declare",
"dijit/Dialog",
"dijit/_Widget",
"dijit/_TemplatedMixin",
"dijit/_WidgetsInTemplateMixin",
"dojo/text!./templates/ConfirmDialog.html",
"dojo/json",
"dojo/_base/lang",
"dojo/on",
"dojo/aspect",
"dijit/form/Button"
],function(
declare, Dialog, _Widget, _TemplatedMixin, _WidgetsInTemplateMixin,
template, json, lang, on, aspect
){
var DialogContentPane = declare([_Widget, _TemplatedMixin, _WidgetsInTemplateMixin], { //........... 1
templateString: template
});  var dialog = declare(Dialog, { //........... 2
title: "警告",
message: "您确认吗?",
preventCache: true,
constructor: function(kwArgs){
lang.mixin(this, kwArgs); this.content = new DialogContentPane({parentDialog: this}); //.............. 3
},
startup: function(){
this.inherited(arguments);
var _this = this;
on(this.content.cancelButton, "click", function(evt){//............ 4
_this.hide();
_this.no();
});
var signal = aspect.after(_this, "onHide", function(){//............. 5
signal.remove();//......... 6
_this.destroyRecursive();//........... 7
}); },
onExecute: function(){//................8
this.yes();
}
}); return dialog;
});

代码说明:

我们需要一个要满足我们需求的Dialog, 但是不用从头构建一个, 所以这里直接继承了"dijit/Dialog", 如代码中 (2) 处. 为了构建出提示内容和两个按钮的界面, 我们要定义dialog的content属性. 见代码(2)处, 我们把自定义的widget实例赋值给content属性. 下面来看一下这个界面模板内容:

<div>

    <div class="dijitDialogPaneContentArea">
<div data-dojo-attach-point="contentNode">${parentDialog.message}</div>
</div> <div class="dijitDialogPaneActionBar"> <button data-dojo-type="dijit.form.Button" type="submit">
OK</button> <button data-dojo-type="dijit.form.Button"
data-dojo-attach-point="cancelButton">Cancel</button> </div> </div>

这个内容很简单, 就是显示要提示的信息(message变量), 还定义了两个按钮.

代码(4)的作用是绑定cancel按钮的click事件, 当点击这个按钮时, 我们要隐藏dialog, 并执行回调函数no()方法. 这个回调函数后面会提到. 代码(5)处, 我们拦截了onHide()方法,目的是当dialog隐藏时一定要销毁该dialog所占用的资源. 这是因为当dialog调用hide()方法时, 只是简单的把style的display置为none而也, 如果不销毁的话, 可能会引起内存溢出. 代码(6)处是取消本次拦截. 代码(7)处是调用dialog的销毁自己资源的方法. 代码(8)是监听dialog中确认按钮(OK)的点击事件, 它调用yes的回调方法.

至此, 整个Dialog的定义已经结束, 下面看下我们怎么使用:

var cd = new ConfirmDialog({
message: "您确定删除此分组?",
yes: function(){
alert("yes...");
},
no: function(){
alert("no...");
}});
cd.show();

跟使用dijit/Dialog一样, 创建一个实例, 但是这里要定义两个回调用方法: yes()no(), 分别是给用户点击了"OK"还是"Cancel"按钮时调用的. 比如, 我们一般可以在这个yes方法里进行一样ajax操作,如果远程删除服务器资源.

结束.

参考引用:

http://stackoverflow.com/questions/10401512/dojo-dialog-with-confirmation-button/10405938#10405938

http://jsfiddle.net/wkydY/205/

dojo使用笔记: 自定义ConfirmDialog的更多相关文章

  1. Dojo初探之2:设置dojoConfig详解,dojoConfig参数详解+Dojo中预置自定义AMD模块的四种方式(基于dojo1.11.2)

    Dojo中想要加载自定义的AMD模块,需要先设置好这个模块对应的路径,模块的路径就是这个模块的唯一标识符. 一.dojoConfig参数设置详解 var dojoConfig = { baseUrl: ...

  2. iOS学习笔记-自定义过渡动画

    代码地址如下:http://www.demodashi.com/demo/11678.html 这篇笔记翻译自raywenderlick网站的过渡动画的一篇文章,原文用的swift,由于考虑到swif ...

  3. dojo框架笔记

    一.模块定义 1.定义只含值对,没有任何依赖的模块(moudle1.js) define({ color: "black", size: "unisize" } ...

  4. Angular JS 学习笔记(自定义服务:factory,Promise 模式异步请求查询:$http,过滤器用法filter,指令:directive)

    刚学没多久,作了一个小项目APP,微信企业号开发与微信服务号的开发,使用的是AngularJS开发,目前项目1.0版本已经完结,但是项目纯粹为了赶工,并没有发挥AngularJS的最大作用,这几天项目 ...

  5. dojo 加载自定义module的路径问题

    因为最近想学学ArcGIS API for JavaScript ,翻了下ESRI的官网guide,发现其是基于dojo框架的,看了两页实在看不懂,于是先来熟悉下dojo框架.人蠢不能怪社会%> ...

  6. [Java] JSP笔记 - 自定义标签

    自定义标签的创建步骤: 自定义标签的四大功能: 自定义标签的类结构: 在 1.0 中呢, 可以将 <body-content> 的值设置为 JSP, 2.0中则不允许在自定义标签体中出现j ...

  7. iOS阶段学习第33天笔记(自定义标签栏(UITabBar)介绍)

    iOS学习(UI)知识点整理 一.自定义标签栏 1.方法一 单个创建标签栏 #import "AppDelegate.h" #import "SecondViewCont ...

  8. AngularJS笔记--自定义指令

    在前端开发中, 我们会遇到很多地方都会用到同一种类型的控件.AngularJS提供了自定义指令功能,我们可以在指令里面定义特定的html模板.提供给前台html调用. 一. 指令的简单定义.  下面定 ...

  9. JavaScript学习笔记-自定义集合类

    //集合类Set( ES6标准才有的类,目前兼容性较差)//自定义集合类:extend = function (o,p){ //定义一个复制对象属性的类函数 for(var x in p){ o[x] ...

随机推荐

  1. Apache Spark源码走读之14 -- Graphx实现剖析

    欢迎转载,转载请注明出处,徽沪一郎. 概要 图的并行化处理一直是一个非常热门的话题,这里头的重点有两个,一是如何将图的算法并行化,二是找到一个合适的并行化处理框架.Spark作为一个非常优秀的并行处理 ...

  2. artDialog ( v 6.0.2 ) content 参数引入页面 html 内容

    /*! artDialog v6.0.2 | https://github.com/aui/artDialog */ 将页面某一隐藏的 div 的 html 内容传到 artdialog 的弹窗中,并 ...

  3. ExtJS笔记5 Components

    参考 :http://blog.csdn.net/zhangxin09/article/details/6914882 An Ext JS application's UI is made up of ...

  4. BAT批处理(二)

    在前一篇中已对BAT批处理基础作了一些总结,但是对于BAT批处理还有很多的知识点没有讲解到,比如DOS中的特殊符号:IF.FOR的使用:变量:更多的DOS命令等等.本文在前一篇的基础上继续对BAT批处 ...

  5. PHP学习(五)----jQuery和JSON数据

    对于jQuery: jQuery 是一个 JavaScript 库. jQuery 极大地简化了 JavaScript 编程.

  6. 1763 An Essay towards solving a Problem in the Doctrine of Chances

    https://en.wikipedia.org/wiki/An_Essay_towards_solving_a_Problem_in_the_Doctrine_of_Chances

  7. pdo封装类

    <?php //http://www.imavex.com/php-pdo-wrapper-class/ class db extends PDO { private $error; priva ...

  8. Java 特殊性领会

    1. 字符串比较绝对不能用 == 而必须是 xx.equals() 2. 多有对象new 后都是以引用的方式存在着 3. 数组list, map 类型都不能边用for 循环边删除,迭代器可以 eg: ...

  9. apex-utility-ai-unity-survival-shooter

    The AI has the following actions available: Action Function Shoot Fires the Kalashnikov Reload Reloa ...

  10. SSH框架中spring的原理

    在ssh项目中,是有明确分工的,spring的作用就相当于将struts和hibernate连接起来,是将两个没有关系的框架的特性,方法,action都放在spring的配置文件中使他们建立关系.取他 ...