简体   繁体   English

如何获取带有data-dojo-attach-point的Dojo节点集的对象描述符?

[英]How can I get the object descriptor for a Dojo node set with data-dojo-attach-point?

In one of my templates I have the following node: 在我的模板之一中,我具有以下节点:

<input data-dojo-attach-point="checkboxIsInternal" type="checkbox" checked />

I want to change the value of another property in the template's class to a certain value when this check is changed. 更改此检查后,我想将模板类中另一个属性的值更改为某个值。 So I thought in modifying the getter and setter for checkboxIsInternal's 'value' property so this happens automatically. 因此,我想到了修改checkboxIsInternal的'value'属性的getter和setter方法,以便自动进行此操作。 Like this: 像这样:

Object.defineProperty(this.checkboxIsInternal, 'value', {
    get: function() { return (this.clientType == 'I'); },
    set: function(v) { this.clientType = (v == 'on' ? 'I' : 'E'); },
    enumerable: true
});

but this hasn't worked. 但这没有用。 So I tried to console.log the descriptors of value property with 所以我尝试用以下命令console.log value属性的描述符

console.log(Object.getOwnPropertyDescriptor(this.checkboxIsInternal, 'value'));

but it showed undefined. 但显示未定义。 Anyone knows why ? 有人知道为什么吗?

If there are any other way to do this (changing one variable based on the change of a node) please let me know. 如果还有其他方法(根据节点的更改来更改一个变量),请告诉我。

I don't know if Object.defineProperty works on an input element's value. 我不知道Object.defineProperty是否对输入元素的值起作用。 If it does, what you're doing there is creating/retrieving expando properties on the checkbox itself, and you want to avoid those. 如果是这样,那么您正在做的事情就是在复选框本身上创建/检索expando属性,并且您想要避免这些。

this.checkboxIsInternal should be a reference to your checkbox element if everything is set up right. 如果一切都设置正确,this.checkboxIsInternal应该是对您的checkbox元素的引用。 Maybe try the old-fashioned way. 也许尝试老式的方法。

this.checkboxIsInternal.onchange = (function(e) {
    this.clientType = (this.checkboxIsInternal.checked) ? "I" : "E";
}).bind(this);

This will update the "clientType" property in your widget whenever the checkbox is checked/unchecked. 每当复选框被选中/取消选中时,这将更新窗口小部件中的“ clientType”属性。

Dijit widgets provide the ability to write custom setters and getters, whose logic is executed when calling the set and get methods. Dijit小部件提供了编写自定义setter和getter的功能,当调用set和get方法时,将执行其逻辑。

Custom setter methods are defined following the pattern _setFooAttr, where “foo” is the name of the property: 定制设置方法是按照_setFooAttr模式定义的,其中“ foo”是属性的名称:

declare(_WidgetBase, {
    foo: "",
    _setFooAttr: function (value) {
        // custom logic, and update the foo property
    }
});

In similar way for can call your custom getter: 以类似的方式可以调用您的自定义getter:

yourWidget.getFooAttr();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM