Nico Schlömer
Nico Schlömer

Reputation: 58951

Overriding `onClick` on a Firefox-Addon ActionButton

When creating a plain old ActionButton in a Firefox Add-on, it seems that one cannot override the onClick property after creation. Anyhow, this

var handleClick = function() {
  tabs.open('https://www.mozilla.org/');
};

var button = new ActionButton({
  id: 'my-link',
  label: 'label me',
  icon: {
    '16': './icon-16.png',
    '32': './icon-32.png',
    '64': './icon-64.png'
  },
  onClick: handleClick
});

button.onClick = function() {
  tabs.open('https://www.github.com/');
};

gives a open a tab with mozilla.org, not github.com.

How to adapt onClick after button creation?

Upvotes: 0

Views: 303

Answers (2)

Nico Schlömer
Nico Schlömer

Reputation: 58951

The easiest thing is probably not to replace the handler but its data:

var url = 'https://www.mozilla.org/';
var handleClick = function() {
  tabs.open(url);
};

// ...

url = 'https://www.github.com/';

Upvotes: 0

Alex McMillan
Alex McMillan

Reputation: 17962

The API Docs suggest using on which is a common pattern for adding event listeners.

button.on('click', handleClick);

although this will add another click handler, not replace the original handler. Hang onto a reference to the handler (handleClick for example) and use button.removeListener(handleClick) to remove the old one.

Upvotes: 1

Related Questions