当前位置 : 主页 > 网页制作 > Dojo >

dojo小例子(34)带Ajax异步验证功能的ValidationTextBox

来源:互联网 收集:自由互联 发布时间:2021-06-15
define([ "dojo/_base/declare", "dijit/form/ValidationTextBox","dojo/request"], function(declare, ValidationTextBox, request) {return declare("AjaxValidationTextBox", [ ValidationTextBox ], {ajaxUrl : null,ajaxLock : false, // 钩子,防止出
define([ 
	"dojo/_base/declare", 
	"dijit/form/ValidationTextBox",
	"dojo/request"
], function(declare, ValidationTextBox, request) {
	return declare("AjaxValidationTextBox", [ ValidationTextBox ], {
		ajaxUrl : null,
		ajaxLock : false, // 钩子,防止出现死循环
		ajaxResult : false,
		oldValue : null, // 用于对比输入值是否有变化
		changed : false, // 标识:输入值是否有变化

		isValid : function() {
			var widget = this;
			this.changed = false;

			if (this.get('value') != this.oldValue) {
				this.changed = true;
			}
			if (!this.ajaxLock && this.ajaxUrl && this.get('value') && !this.focused && this.changed) {
				this.oldValue = this.get('value');
				this.ajaxLock = true;

				request(widget.ajaxUrl + encodeURIComponent( widget.get('value') ), {
					handleAs : 'text'
				}).then(function(result) {
					widget.ajaxResult = (result == "true" ? true : false);
					widget.validate(); // 会调用isValid方法重新校验输入的值,弹出tip
					widget.ajaxLock = false;
				});
			}

			return this.ajaxResult;
		}
	});

});

用法:

<input data-dojo-type="my/AjaxValidationTextBox" data-dojo-props="
				required : true, missingMessage:'编号不能为空!', 
				ajaxUrl:/rest/isNumValid/', 
				invalidMessage:'编号已存在!'"  />
网友评论