amurcia
amurcia

Reputation: 791

Titanium - widget eventListener

I'm using a widget. I'm creating it in the index.js controller. I would like to be able to access the widget's event listeners so I can tell when the value of the widget changes, to change colours in index.js elements.

Can anyone help me?

Like this question

Upvotes: 1

Views: 357

Answers (1)

Fokke Zandbergen
Fokke Zandbergen

Reputation: 3866

All Alloy controllers, including those of Widgets extend Backbone.Events and are therefor event dispatchers. The scenario you talk about is an excellent use case for these. Let me give you an example:

widget.xml

<Alloy>
  <TextField onChange="onTextFieldChange" />
</Alloy>

widget.js

function onTextFieldChange(e) {
  $.trigger('change', {
    value: e.value
  });
}

index.xml

<Alloy>
  <Window id="win">
    <Widget src="myWidget" onChange="onWidgetChange" />
  </Window>
</Alloy>

index.js

function onWidgetChange(e) {
  $.win.backgroundColor = e.value;
}

Upvotes: 3

Related Questions