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:'编号已存在!'" />