Skip to content

Instantly share code, notes, and snippets.

@FokkeZB
Last active Aug 29, 2018
Embed
What would you like to do?
Passing through event listeners to widget view elements in Titanium Alloy.
function doClick(e) {
alert('Clicked!');
}
// Remove the event listener like this:
// $.myWidget.off("click", doClick);
$.index.open();
<Alloy>
<Window>
<Widget id="myWidget" src="my.alloy.widget" doClick="doClick" />
</Window>
</Alloy>
// Overwrite Backbone methods, as used in the generated code by Alloy:
// doClick ? $.__views.myWidget.on("click", doClick) : __defers["$.__views.myWidget!click!doClick"] = !0;
exports.on = $.label.addEventListener;
exports.off = $.label.removeEventListener;
// Overwrite backbone aliasses:
exports.bind = $.label.addEventListener;
exports.unbind = $.label.removeEventListener;
// Support Titanium methods
exports.addEventListener = $.label.addEventListener;
exports.removeEventListener = $.label.removeEventListener;
// Overwrite Backbone trigger and Titanium fireEvent methods for convenience
exports.trigger = $.label.fireEvent;
exports.fireEvent = $.label.fireEvent;
<Alloy>
<Label>Hello World</Label>
</Alloy>
@budowski
Copy link

budowski commented Mar 12, 2014

OK, this seemed to solve it on Android as well (I'm pretty sure it's not the best way to work around it):

exports.on = function(name, cb) { return $.label.addEventListener(name, cb); };
exports.off = function(name, cb) { return $.label.removeEventListener(name, cb); };
exports._hasListenersForEventType = function(name, flag) {
    return $.label._hasListenersForEventType(name, flag);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment