Quantcast
Channel: Adobe Community : Popular Discussions - Illustrator Scripting
Viewing all articles
Browse latest Browse all 12845

Add event handler as/via prototype?

$
0
0

I would like to try and increase my javascript knowledge here.

So, here is what I have got so far, and I would like to make it better. I saw that if you click on the 1 pixel in the border of a dropdown list in CC ScriptUI, it will have the undesired effect of selecting "null", which crashed some of my scripts. Therefore I have made this "prototype" that selects the first item, should someone trigger the effect of selecting null by inadvertently clicking on the border of the dropdownlist.

 

DropDownList.prototype.selectWell = function(){

  //CC will let you select null

  this.addEventListener('change', function(){

    if(this.selection == null){

      this.selection = this.items[0];

    }

  });

}

 

The way it would work is like this:

 

var d = window.add("dropdownlist", undefined, ["Item 1","Item 2"]);

d.selectWell();

 

Okay, this works, but then you gotta write "d.selectWell();" after every dropdown.

Would it be possible to write such code that every new instance of a dropdownlist already comes with this event listener added?

 

 

In browser javascript, they have querySelector(element) to add one listener to every element of the desired kind. I am not sure there is such available in ScriptUI.

I suppose it would be possible to do mass listener assignments via recursive function, but would need to be repeated in windows where new dropdowns are added dynamically.
Also, I could make my own constructor, call it SuperDropDownList and in that constructor add all the necessary listeners and use it to add any dropdown that I want to have the custom functionality in.

However, if this could be done with a simple "DropDownList.prototype", I would like to learn how to do so!


Viewing all articles
Browse latest Browse all 12845

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>