website_form.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * Client code for the website_form template.
  3. *
  4. * - events
  5. */
  6. Template.website_form.events({
  7. "click .js-toggle-website-form": function () {
  8. $("#website_form").toggle("slow");
  9. },
  10. "click #site-lookup": function (event) {
  11. event.preventDefault();
  12. const url = $(event.target).parent().prev().val();
  13. Meteor.call("loadSite", url, function (error, result) {
  14. let $result = $("<root>" + result + "</root>");
  15. let titleElement = $($result.find("title")[0]).text();
  16. let metaTitle = $result.find("meta[name=\"title\"]").attr("content");
  17. let metaDescription = $result.find("meta[name=\"description\"]").attr("content");
  18. let title = [titleElement, metaTitle].join(" / ");
  19. $("#title").val(title);
  20. $("#description").val(metaDescription);
  21. });
  22. // Stop the form submit from reloading the page
  23. return false;
  24. },
  25. "submit .js-save-website-form": function (event) {
  26. // here is an example of how to get the url out of the form:
  27. const url = event.target.url.value;
  28. const title = event.target.title.value;
  29. const description = event.target.description.value;
  30. let poster = {
  31. _id: Meteor.userId(),
  32. name: Meteor.user().username
  33. };
  34. const postDate = new Date();
  35. // put your website saving code in here!
  36. const doc = { url, title, description, poster, postDate };
  37. Meteor._debug("Client trying to insert", doc);
  38. Websites.insert(doc);
  39. // Stop the form submit from reloading the page
  40. return false;
  41. }
  42. });