Reputation: 16164
In appcelerator titanium, is it possible to have an event listener on a function call?
Sort of like
var coolManDool = function(){...};
coolManDool.addEventListener('what goes here?", function(){ ... } );
i'd like to be able to wrap a certain group of functions in such a way to ensure they do something. Future code might expand on the doing of the something, and having a central spot in my code where this is controlled would be really nice.
Upvotes: 2
Views: 3583
Reputation: 5332
You can not add an eventListener to any functions but you can fire an event inside your function and also use your function as callback for an event. An Event listener is used to handle events. In your case, if you want to add events to functions, you can simply create custom events in Titanium. For example if you want to perform some particular actions while calling a function, you can simply do it as follows.
//Creating the custom event
window.addEventListener('myEvent', function(){
alert('function called')
});
function foo(){
//Some actions
window.fireEvent('myEvent');
}
You can also add event to the application itself(Application-level events). App-level events are global to your app. They are accessible in all contexts, functional scopes, CommonJS modules, and so forth. You can fire them and listen for them via the Ti.App module.
Ti.App.addEventListener('myAppEvent', function(){
alert('Application level event get fired');
});
//Fire the event like
Ti.App.fireEvent('myAppEvent');
Please refer Event handling in Titanium for more details
Upvotes: 5