ESRI API Dojo \ on VS data-dojo-attach-event

748
1
Jump to solution
09-29-2016 03:11 AM
AlbertoLópez
New Contributor III

Hello;

I am studying the ArcGIS API javascript to create widgets and I don`t understand what is the difference between Dojo \ on in the Widget.js file and data-dojo-attach-event in the template.html file.

My questions:

When Dojo \ on is used and when data-dojo-attach-event?

What is the difference between these two snippets of code?

In Widget.js file:

on(this.alertButton, "click", myFunction ())
myFunction () {
topic.publish("alertUser", "I am alerting you.");
}

In template.html:

<button data-dojo-attach-point= 'alertButton' data-dojo-attach-event='click: myFunction '>press me</button>

Thanks.

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Alberto,

   There are going to be limited times when you will use data-dojo-attach-event. The are a couple of reason for this:

  1. You will have to have a static widget template, many times a widgets template is dynamically adjusted.
  2. You can not pause or remove that dijits event.
  3. Once your template is established it is easier to just edit code based functionality in one place (the js file).
  4. As people read your code it is harder to figure out where the attached event is coming from as it is in a separate file.

When it comes down to the two snippets of code the function almost identically so there is little to no difference. It is more of a preference.

View solution in original post

1 Reply
RobertScheitlin__GISP
MVP Emeritus

Alberto,

   There are going to be limited times when you will use data-dojo-attach-event. The are a couple of reason for this:

  1. You will have to have a static widget template, many times a widgets template is dynamically adjusted.
  2. You can not pause or remove that dijits event.
  3. Once your template is established it is easier to just edit code based functionality in one place (the js file).
  4. As people read your code it is harder to figure out where the attached event is coming from as it is in a separate file.

When it comes down to the two snippets of code the function almost identically so there is little to no difference. It is more of a preference.