Browse Source

clean up js example

pull/2709/head
David Lord 7 years ago
parent
commit
1e84c67beb
No known key found for this signature in database
GPG Key ID: 7A1C87E3F5BC42A8
  1. 2
      docs/patterns/jquery.rst
  2. 2
      examples/javascript/js_example/templates/base.html
  3. 5
      examples/javascript/js_example/templates/fetch.html
  4. 4
      examples/javascript/js_example/templates/jquery.html
  5. 5
      examples/javascript/js_example/templates/plain.html

2
docs/patterns/jquery.rst

@ -163,5 +163,5 @@ explanation of the little bit of code above:
we set earlier. we set earlier.
Check out the :gh:`example source <examples/javascript>` for a full Check out the :gh:`example source <examples/javascript>` for a full
application demonstrating the jQuery on this page, as well as the same application demonstrating the code on this page, as well as the same
thing using ``XMLHttpRequest`` and ``fetch``. thing using ``XMLHttpRequest`` and ``fetch``.

2
examples/javascript/js_example/templates/base.html

@ -23,7 +23,7 @@
<hr> <hr>
<p>{% block intro %}{% endblock %}</p> <p>{% block intro %}{% endblock %}</p>
<hr> <hr>
<form> <form id="calc">
<label>a <input name="a"></label> <label>a <input name="a"></label>
<span>+</span> <span>+</span>
<label>b <input name="b"></label> <label>b <input name="b"></label>

5
examples/javascript/js_example/templates/fetch.html

@ -13,7 +13,7 @@
<script> <script>
function addSubmit(ev) { function addSubmit(ev) {
ev.preventDefault(); ev.preventDefault();
fetch('{{ url_for('add') }}', { fetch({{ url_for('add')|tojson }}, {
method: 'POST', method: 'POST',
body: new FormData(this) body: new FormData(this)
}) })
@ -30,6 +30,7 @@
span.innerText = data.result; span.innerText = data.result;
} }
document.forms[0].addEventListener('submit', addSubmit); var form = document.getElementById('calc');
form.addEventListener('submit', addSubmit);
</script> </script>
{% endblock %} {% endblock %}

4
examples/javascript/js_example/templates/jquery.html

@ -13,7 +13,7 @@
ev.preventDefault(); ev.preventDefault();
$.ajax({ $.ajax({
method: 'POST', method: 'POST',
url: '{{ url_for('add') }}', url: {{ url_for('add')|tojson }},
data: $(this).serialize() data: $(this).serialize()
}).done(addShow); }).done(addShow);
} }
@ -22,6 +22,6 @@
$('#result').text(data.result); $('#result').text(data.result);
} }
$('form:first').on('submit', addSubmit); $('#calc').on('submit', addSubmit);
</script> </script>
{% endblock %} {% endblock %}

5
examples/javascript/js_example/templates/plain.html

@ -12,7 +12,7 @@
ev.preventDefault(); ev.preventDefault();
var request = new XMLHttpRequest(); var request = new XMLHttpRequest();
request.addEventListener('load', addShow); request.addEventListener('load', addShow);
request.open('POST', '{{ url_for('add') }}'); request.open('POST', {{ url_for('add')|tojson }});
request.send(new FormData(this)); request.send(new FormData(this));
} }
@ -22,6 +22,7 @@
span.innerText = data.result; span.innerText = data.result;
} }
document.forms[0].addEventListener('submit', addSubmit); var form = document.getElementById('calc');
form.addEventListener('submit', addSubmit);
</script> </script>
{% endblock %} {% endblock %}

Loading…
Cancel
Save