自定义 dojo 小部件不会在单击按钮时清除文本字段

Custom dojo widget won't clear text field upon button click

单击清除按钮后,它会弹出警报并且 this.gridQuery.value 中没有显示任何内容,但 gridQuery 输入字段本身不会反映更改。有什么想法吗?

define([
    "dojo/_base/declare",
    "dojo/request",
    "dijit/_WidgetBase",
    "dijit/_TemplatedMixin",
    "dijit/_WidgetsInTemplateMixin"
], function(declare, request, _WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin){
    return declare([_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], {
        //  set our template
        templateString: '<div>' +
        'Search: <div id="gridQuery" data-dojo-props="intermediateChanges: true" data-dojo-attach-event="onChange: search" data-dojo-attach-point="gridQuery" data-dojo-type="dijit/form/TextBox"></div> ' +
        '<button data-dojo-attach-point="gridClear" data-dojo-attach-event="onclick: clear">Clear</button>' + 
        '<div data-dojo-attach-point="gridText"></div>' 
        + '</div>',

        clear: function() {         
            this.gridQuery.value = "";
            alert("Should be empty: " + this.gridQuery.value);
        },

        search: function() {
            this.gridText.innerHTML = this.gridQuery.value;
        },

        postCreate: function() {

        }
    });
});

您没有看到任何效果,因为您没有正确设置 Dijit 小部件的值。

而不是:

this.gridQuery.value = '';

尝试:

this.gridQuery.set('value', '');

由于没有完全跨浏览器的方式来观察属性的变化,Dijit 的 getset API 允许围绕属性的检索和修改实现自定义逻辑,并使其成为对 watch 的变化做出反应也很简单。