Advertisement
tochka

03. Messenger

Jul 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. function attachEvents() {
  2. let url = `https://messenger-9dfc1.firebaseio.com/`;
  3. $('#submit').on('click', function () {
  4. let authorName = $('#author').val();
  5. let messageContent = $('#content').val();
  6.  
  7. let obj = {
  8. author: authorName,
  9. content: messageContent,
  10. timestamp: Date.now()
  11. };
  12.  
  13. $.ajax({
  14. method: "POST",
  15. url: url + '.json',
  16. data: JSON.stringify(obj)
  17. });
  18.  
  19. $('#author').val('');
  20. $('#content').val('');
  21. });
  22.  
  23.  
  24. $('#refresh').on('click', function () {
  25. $('#messages').empty();
  26. $.ajax({
  27. method: "GET",
  28. url: url + '.json'
  29. }).then(function func(info) {
  30. Object.keys(info)
  31. .forEach(function eachKey(key) {
  32. let id = key;
  33. let author = info[id].author;
  34. let message = info[id].content;
  35.  
  36. $('#messages').append(`${author}: ${message}\n`)
  37. });
  38. })
  39. });
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement