Snippet for a better alert

I’m lazy, so I rather type:

alert('Hello!');

then:

Ti.UI.createAlertDialog({message: 'Hello!'}).show();

Custom title

However, for localized apps or if for other reasons I want to have another title and/or button label then the default Alert and OK, this doesn’t work. So in my classic app.js or Alloy alloy.js I have:

function alert(message, title, ok) {
  Ti.UI.createAlertDialog({
    message: message,
    title: title || L('alert_title', 'Alert'),
    ok: ok || L('alert_ok', 'OK')
  }).show();
}

This will override the default alert() method for you to use anywhere you want, while of course still respecting calls that lack the second argument. By using L() I can use i18n to localize title and ok.

Confirm dialogs

If you need a confirm dialog, you can use Alloy’s built-in for that, either just where you need it or by making it globally available through:

confirm = require('alloy/dialogs').confirm;

App imagineer: Imagining, Engineering & Speaking about Native mobile Apps with Appcelerator Titanium & Alloy • Meetup organizer • Certified Expert • Titan


Comments