Refs #31493 -- Replaced var with const/let in documentation JS.

This commit is contained in:
Adam Johnson 2020-06-24 11:41:10 +02:00 committed by Carlton Gibson
parent 30e59705fc
commit 2afa61e7d9
4 changed files with 34 additions and 25 deletions

View File

@ -56,13 +56,13 @@ Django's JavaScript tests use `QUnit`_. Here is an example test module:
QUnit.module('magicTricks', { QUnit.module('magicTricks', {
beforeEach: function() { beforeEach: function() {
var $ = django.jQuery; const $ = django.jQuery;
$('#qunit-fixture').append('<button class="button"></button>'); $('#qunit-fixture').append('<button class="button"></button>');
} }
}); });
QUnit.test('removeOnClick removes button on click', function(assert) { QUnit.test('removeOnClick removes button on click', function(assert) {
var $ = django.jQuery; const $ = django.jQuery;
removeOnClick('.button'); removeOnClick('.button');
assert.equal($('.button').length, 1); assert.equal($('.button').length, 1);
$('.button').click(); $('.button').click();
@ -70,7 +70,7 @@ Django's JavaScript tests use `QUnit`_. Here is an example test module:
}); });
QUnit.test('copyOnClick adds button on click', function(assert) { QUnit.test('copyOnClick adds button on click', function(assert) {
var $ = django.jQuery; const $ = django.jQuery;
copyOnClick('.button'); copyOnClick('.button');
assert.equal($('.button').length, 1); assert.equal($('.button').length, 1);
$('.button').click(); $('.button').click();

View File

@ -85,11 +85,11 @@ You can acquire the token like this:
.. code-block:: javascript .. code-block:: javascript
function getCookie(name) { function getCookie(name) {
var cookieValue = null; let cookieValue = null;
if (document.cookie && document.cookie !== '') { if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';'); const cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) { for (let i = 0; i < cookies.length; i++) {
var cookie = cookies[i].trim(); const cookie = cookies[i].trim();
// Does this cookie string begin with the name we want? // Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) { if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
@ -99,14 +99,14 @@ You can acquire the token like this:
} }
return cookieValue; return cookieValue;
} }
var csrftoken = getCookie('csrftoken'); const csrftoken = getCookie('csrftoken');
The above code could be simplified by using the `JavaScript Cookie library The above code could be simplified by using the `JavaScript Cookie library
<https://github.com/js-cookie/js-cookie/>`_ to replace ``getCookie``: <https://github.com/js-cookie/js-cookie/>`_ to replace ``getCookie``:
.. code-block:: javascript .. code-block:: javascript
var csrftoken = Cookies.get('csrftoken'); const csrftoken = Cookies.get('csrftoken');
.. note:: .. note::
@ -137,7 +137,7 @@ and read the token from the DOM with JavaScript:
{% csrf_token %} {% csrf_token %}
<script> <script>
var csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value; const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;
</script> </script>
Setting the token on the AJAX request Setting the token on the AJAX request
@ -148,7 +148,7 @@ Finally, you'll need to set the header on your AJAX request. Using the
.. code-block:: javascript .. code-block:: javascript
var request = new Request( const request = new Request(
/* URL */, /* URL */,
{headers: {'X-CSRFToken': csrftoken}} {headers: {'X-CSRFToken': csrftoken}}
); );

View File

@ -1830,7 +1830,7 @@ The resulting data can be accessed in JavaScript like this:
.. code-block:: javascript .. code-block:: javascript
var value = JSON.parse(document.getElementById('hello-data').textContent); const value = JSON.parse(document.getElementById('hello-data').textContent);
XSS attacks are mitigated by escaping the characters "<", ">" and "&". For XSS attacks are mitigated by escaping the characters "<", ">" and "&". For
example if ``value`` is ``{'hello': 'world</script>&amp;'}``, the output is: example if ``value`` is ``{'hello': 'world</script>&amp;'}``, the output is:

View File

@ -1080,9 +1080,12 @@ interface within your Python code::
The ``ngettext`` function provides an interface to pluralize words and The ``ngettext`` function provides an interface to pluralize words and
phrases:: phrases::
var object_count = 1 // or 0, or 2, or 3, ... const objectCount = 1 // or 0, or 2, or 3, ...
s = ngettext('literal for the singular case', const string = ngettext(
'literal for the plural case', object_count); 'literal for the singular case',
'literal for the plural case',
objectCount
);
``interpolate`` ``interpolate``
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~
@ -1096,23 +1099,29 @@ function supports both positional and named interpolation:
corresponding ``fmt`` placeholders in the same order they appear. corresponding ``fmt`` placeholders in the same order they appear.
For example:: For example::
fmts = ngettext('There is %s object. Remaining: %s', const formats = ngettext(
'There are %s objects. Remaining: %s', 11); 'There is %s object. Remaining: %s',
s = interpolate(fmts, [11, 20]); 'There are %s objects. Remaining: %s',
// s is 'There are 11 objects. Remaining: 20' 11
);
const string = interpolate(formats, [11, 20]);
// string is 'There are 11 objects. Remaining: 20'
* Named interpolation: This mode is selected by passing the optional * Named interpolation: This mode is selected by passing the optional
boolean ``named`` parameter as ``true``. ``obj`` contains a JavaScript boolean ``named`` parameter as ``true``. ``obj`` contains a JavaScript
object or associative array. For example:: object or associative array. For example::
d = { const data = {
count: 10, count: 10,
total: 50 total: 50
}; };
fmts = ngettext('Total: %(total)s, there is %(count)s object', const formats = ngettext(
'there are %(count)s of a total of %(total)s objects', d.count); 'Total: %(total)s, there is %(count)s object',
s = interpolate(fmts, d, true); 'there are %(count)s of a total of %(total)s objects',
data.count
);
const string = interpolate(formats, data, true);
You shouldn't go over the top with string interpolation, though: this is still You shouldn't go over the top with string interpolation, though: this is still
JavaScript, so the code has to make repeated regular-expression substitutions. JavaScript, so the code has to make repeated regular-expression substitutions.