Skip to content

Instantly share code, notes, and snippets.

@hansemannn
Last active October 15, 2019 20:16
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hansemannn/f9c46ac5f49362e337b1b6c69701da9e to your computer and use it in GitHub Desktop.
Save hansemannn/f9c46ac5f49362e337b1b6c69701da9e to your computer and use it in GitHub Desktop.
Using ES7+ async/await in Appcelerator Titanium
export function showOptions (args) {
const title = args.title;
const message = args.message;
const options = args.options;
const destructive = args.destructive !== undefined ? args.destructive : -1;
let cancel = -1;
return new Promise((resolve, reject) => {
if (OS_IOS) {
options.push('Cancel');
cancel = options.length - 1;
}
const optionDialog = Ti.UI.createOptionDialog({ title, message, options, cancel, destructive });
optionDialog.addEventListener('click', event => {
if ((OS_ANDROID && event.cancel) || (OS_IOS && event.index === optionDialog.cancel)) {
resolve({ index: -1 });
return;
};
resolve(event);
});
optionDialog.show();
});
};
import { showOptions } from 'app-utils';
const button = Ti.UI.createButton({ title: 'Show options' });
const window = Ti.UI.createWindiow({ backgroundColor: 'white' });
button.addEventListener('click', async () => {
const params = { options: [ 'Take photo', 'Choose existing' ] };
const event = await showOptions(params);
if (event.index === 0) {
takePhoto();
} else if (event.index === 1) {
selectPhoto();
}
});
window.add(button);
window.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment