Last active
October 15, 2019 20:16
-
-
Save hansemannn/f9c46ac5f49362e337b1b6c69701da9e to your computer and use it in GitHub Desktop.
Using ES7+ async/await in Appcelerator Titanium
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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