I have problems to retrieve a button-widget with dojo/registry.
I have a widget and inside it's postCreate
-function some other functions are called. In the last one registry.byId()
returns "undefined" causing an error. The element with the provided id does exist in the html-template.
I have also created an very simple working version in JSFiddle and even if I try this variant in my code I get "undefined". EDIT: updated Fiddle
Here is a short summary of the code with the original use of registry.byId()
in the _setupButton-function and also with the solution from the JSFiddle before the declare
-part. Both versions return "undefined":
define(["dojo/_base/declare", "dojo/_base/array", "dojo/_base/lang", "dojo/dom-class", "dojo/number", "dojo/parser", "dojo/ready", "dojo/dom-construct", "dijit/registry", "dijit/form/NumberTextBox", "dijit/_Widget", "dijit/_TemplatedMixin", "dijit/Menu", "dijit/MenuItem", "dojo/text!./templates/template.html"],
function(declare, array, lang, domClass, number, parser, ready, domconstruct, registry, NumberTextBox, _Widget, _TemplatedMixin, Menu, MenuItem, templateStringContent) {
parser.parse();
ready(function () {
console.info(registry.byId("startCalculationButton"));
});
return declare([_Widget, _TemplatedMixin],
{
templateString: templateStringContent,
postCreate() {
this._setupButton();
},
_setupButton: function() {
buttonWidget = registry.byId("startCalculationButton");
buttonWidget.set("disabled", true);
},
});
});
The html is the same as in the provided JSFiddle-Snippet. Other id's (for toher elements) also won't work.
So why registry.byId()
possibly won't work for me? I'm new to AMD-style and the code already worked with the old syntax using dijit.byId
.
Edit:
I figured out that the widget is not registered in the registry-object. What could be the reason for that? If the template could not be found I would get a error 404, so this reason we can exclude.
Edit2:
Also document.getElementById()
will just return null
. I thought passing the templateString would be enough and dojo does everything else?
According to the dojo documentation the widget needs besides
_TemplatedMixin
also_WidgetsInTemplateMixin
and inherit from it:Since the dijit-button will be a widget inside my template so adding
_WidgetsInTemplateMixin
and inheriting from it solved my problem without the need of other changes. My code looks now something like this (shortened):