Communication between widget.js and widget.html

4570
6
Jump to solution
02-11-2015 11:37 AM
TimWitt2
MVP Regular Contributor

Hey everybody,

If I create a textbox or a button in my widget.html file, how can I access these in my widget.js file?

For example if I create a button in my widget.html file with the id="TestButton", how would I write a button click event in my widget.js file?

Thanks!

Tim

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Esteemed Contributor

Tim,

  Use the dojo attach point name and not the dom id.

on(this.button, 'click', lang.hitch(this, function(evt){
    console.log("It is clear!")
}));

View solution in original post

0 Kudos
6 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Tim,

  Just use

on(this.TestButton, 'click', lang.hitch(this, function(evt){
    //todo
}));
TimWitt2
MVP Regular Contributor

Thanks Robert,

I get the following error:

fail to open widget Demo. TypeError: Cannot read property 'on' of undefined

Here is what I have in my widget.js

define(['dojo/_base/declare', 'jimu/BaseWidget', 'dojo/dom', "dojo/on",'dojo/_base/lang'],
function(declare, BaseWidget, dom, on, lang) {
  //To create a widget, you need to derive from BaseWidget.
  return declare([BaseWidget], {
    // DemoWidget code goes here 


    //please note that this property is be set by the framework when widget is loaded.
    //templateString: template,


    baseClass: 'jimu-widget-demo',


    postCreate: function() {
      this.inherited(arguments);
      console.log('postCreate');
    },


    startup: function() {
      this.inherited(arguments);
      this.mapIdNode.innerHTML = 'map id:' + this.map.id;
      console.log('startup');
    },


    onOpen: function(){
      console.log('It is open, come on in!');
   on(this.clear, 'click', lang.hitch(this, function(evt){  
  console.log("It is clear!")
  }));


    },


    onClose: function(){
      console.log('onClose');
   var test = this;
    },


    onMinimize: function(){
      console.log('onMinimize');
    },


    onMaximize: function(){
      console.log('onMaximize');
    },


    onSignIn: function(credential){
      /* jshint unused:false*/
      console.log('onSignIn');
    },


    onSignOut: function(){
      console.log('onSignOut');
    }
  });
});

And here is what I have in my widget.html:

<div>
<button id="clear" data-dojo-type="dijit/form/Button" type="button" data-dojo-attach-point="button">Clear All</button> 
</div>

Any ideas what I did wrong?

Tim

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Tim,

  Use the dojo attach point name and not the dom id.

on(this.button, 'click', lang.hitch(this, function(evt){
    console.log("It is clear!")
}));
0 Kudos
TimWitt2
MVP Regular Contributor

Robert works like a charm!

What would I do if I have more than one button and text fields?

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Give each control a unique data-dojo-attach-point name. You can also add the event handler stuff directly in the html template.

data-dojo-attach-point="list" data-dojo-attach-event="click:_selectResultItem,mouseover:_overResultItem,mouseout:_outResultItem"

TimWitt2
MVP Regular Contributor

Thanks Robert that makes sense.

0 Kudos