You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 2.5KB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. $('#contactForm').submit(function(event) {
  2. // prevent page reload
  3. event.preventDefault();
  4. var name = $('#name').val();
  5. var email = $('#email').val();
  6. var mobile = $('#mobile').val();
  7. // Save the contact to the database with Hoodie
  8. hoodie.store.add({
  9. name: name,
  10. mobile: mobile,
  11. email: email
  12. });
  13. $('#contactForm')[0].reset();
  14. });
  15. $('#signup').click(function (event) {
  16. signUp();
  17. });
  18. $('#signin').click(function (event) {
  19. signIn();
  20. });
  21. $('#signout').click(function (event) {
  22. signOut();
  23. });
  24. if (hoodie.account.isSignedIn()) {
  25. showAuthenticated();
  26. } else {
  27. showAnonymous();
  28. }
  29. // when the site loads in the browser,
  30. // we load all previously saved contacts from hoodie
  31. loadContacts();
  32. //when a new entry is added to the database, run the corresponding function
  33. hoodie.store.on('add', addNewContactToList);
  34. function loadContacts() {
  35. hoodie.store.findAll().then(function(contacts) {
  36. var tbody = '';
  37. $.each(contacts, function (i, contact) {
  38. var row = '<tr><td>' + contact.name + '</td><td>' + contact.mobile + '</td><td>' + contact.email + '</td></tr>';
  39. tbody += row;
  40. });
  41. $("#contactList tbody").html('').html(tbody);
  42. });
  43. }
  44. function addNewContactToList(contact) {
  45. var newContact = '<tr><td>' + contact.name + '</td><td>' + contact.mobile + '</td><td>' + contact.email + '</td></tr>'
  46. $("#contactList tbody").append(newContact);
  47. }
  48. function signUp() {
  49. var username = prompt('username');
  50. var password = prompt('password');
  51. hoodie.account.signUp({
  52. username: username,
  53. password: password
  54. })
  55. .then(function() {
  56. return hoodie.account.signIn({
  57. username: username,
  58. password: password
  59. });
  60. })
  61. .then(function() {
  62. showAuthenticated();
  63. })
  64. .catch(function(errror) {
  65. alert('Ooops, something went wrong: ' + error.message);
  66. })
  67. }
  68. function signIn(){
  69. var username = prompt('username');
  70. var password = prompt('password');
  71. hoodie.account.signIn({
  72. username: username,
  73. password: password
  74. })
  75. .then(function() {
  76. showAuthenticated();
  77. })
  78. .catch(function(error) {
  79. alert('ooops: ' + error.message);
  80. });
  81. }
  82. function signOut(){
  83. hoodie.account.signOut()
  84. .then(function() {
  85. showAnonymous();
  86. })
  87. .catch(function(error) {
  88. alert('ooops: ' + error.message);
  89. });
  90. }
  91. function showAuthenticated(){
  92. $('#username').text('signed in as ' + hoodie.account.username);
  93. $('#authenticated').show();
  94. $('#anonymous').hide();
  95. }
  96. function showAnonymous(){
  97. $('#authenticated').hide();
  98. $('#anonymous').show();
  99. }