Advertisement
exadel

JavaScript from 7/23/14 Appery.io Webinar

Jul 25th, 2014
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. // Cheatsheet from:
  2. // Incorporating Native Device APIs and Sensors in Your Apps (July 23, 2014)
  3. // http://youtu.be/mTz5BdqKlAQ [recording]
  4. Beep
  5. navigator.notification.beep(2);
  6.  
  7. Notifications
  8. navigator.notification.alert(
  9. 'Build apps fast with Appery.io',
  10. null,
  11. 'My App',
  12. 'Got it.'
  13. );
  14.  
  15. function alertDismissed () {
  16. alert ("Nice - that worked");
  17. }
  18.  
  19. Geolocation
  20. var onSuccess = function(position) {
  21.  
  22. Apperyio("geolist1").text(position.coords.latitude);
  23. Apperyio("geolist2").text(position.coords.longitude);
  24. };
  25.  
  26. function onError(error) {
  27. alert('code: ' + error.code + '\n' +
  28. 'message: ' + error.message + '\n');
  29. }
  30.  
  31. navigator.geolocation.getCurrentPosition(onSuccess, onError);
  32.  
  33.  
  34. Accelerometer
  35. $t.AccelerometerImplementation = $t.createClass(null, {
  36.  
  37. init: function(requestOptions) {
  38. this.__requestOptions = $.extend({}, requestOptions);
  39. },
  40.  
  41. process: function(settings) {
  42. if (this.__requestOptions.echo) {
  43. settings.success(this.__requestOptions.echo);
  44. } else {
  45.  
  46. var watchID = localStorage.getItem("watchID"); //get the watchID variable
  47.  
  48. if (watchID) { //check if it already exists or not
  49. navigator.accelerometer.clearWatch(watchID); //if it exists clear and stop watch
  50. watchID = null;
  51. localStorage.removeItem("watchID"); //remove this variable from the local storage
  52.  
  53. $('[dsid=stop_watch_button]').find('.ui-btn-inner').text('Start watch'); //change button text to "Start watch"
  54. } else {
  55. //if it is not exists start watch for accelerometer data
  56. var options = { frequency: parseInt(settings.data.frequency)};
  57. watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options); //create new watchID
  58.  
  59. localStorage.setItem("watchID", watchID); //save new watchID to the local storage
  60.  
  61. $('[dsid=stop_watch_button]').find('.ui-btn-inner').text('Stop watch'); //change button text to "Stop watch"
  62. }
  63. }
  64.  
  65. function onSuccess(acceleration) {
  66.  
  67. var accelerometerData = 'AccelerationX: ' + acceleration.x + '\n'
  68. +'AccelerationY: ' + acceleration.y + '\n'
  69. +'AccelerationZ: ' + acceleration.z + '\n'
  70. +'Timestamp: ' + acceleration.timestamp;
  71.  
  72. settings.success({'accelerometerData': accelerometerData});
  73.  
  74. }
  75.  
  76. //This fires in case of error
  77. function onError() {
  78. var accelerometerData = 'Error!';
  79. settings.success({'accelerometerData': accelerometerData});
  80. }
  81.  
  82. settings.complete('success');
  83.  
  84. }
  85.  
  86. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement