1
0
mirror of https://github.com/django/django.git synced 2025-07-16 15:49:12 +00:00

[3.0.x] Refs #31493 -- Replaced var with const/let in documentation JS.

Backport of 2afa61e7d99b2ff2656dc64b6e28db88baf786a4 from master

Co-authored-by: Carlton Gibson <carlton.gibson@noumenal.es>
This commit is contained in:
Adam Johnson 2020-06-24 11:41:10 +02:00 committed by Carlton Gibson
parent 453a5bf302
commit af2e95b0fa
4 changed files with 33 additions and 24 deletions

View File

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

View File

@ -85,11 +85,11 @@ You can acquire the token like this:
.. code-block:: javascript
function getCookie(name) {
var cookieValue = null;
let cookieValue = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].trim();
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
@ -99,14 +99,14 @@ You can acquire the token like this:
}
return cookieValue;
}
var csrftoken = getCookie('csrftoken');
const csrftoken = getCookie('csrftoken');
The above code could be simplified by using the `JavaScript Cookie library
<https://github.com/js-cookie/js-cookie/>`_ to replace ``getCookie``:
.. code-block:: javascript
var csrftoken = Cookies.get('csrftoken');
const csrftoken = Cookies.get('csrftoken');
.. note::
@ -138,7 +138,7 @@ and read the token from the DOM with JavaScript:
{% csrf_token %}
<script type="text/javascript">
// using jQuery
var csrftoken = jQuery("[name=csrfmiddlewaretoken]").val();
const csrftoken = jQuery("[name=csrfmiddlewaretoken]").val();
</script>
Setting the token on the AJAX request

View File

@ -1816,7 +1816,7 @@ The resulting data can be accessed in JavaScript like this:
.. 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
example if ``value`` is ``{'hello': 'world</script>&amp;'}``, the output is:

View File

@ -1067,9 +1067,12 @@ interface within your Python code::
The ``ngettext`` function provides an interface to pluralize words and
phrases::
var object_count = 1 // or 0, or 2, or 3, ...
s = ngettext('literal for the singular case',
'literal for the plural case', object_count);
const objectCount = 1 // or 0, or 2, or 3, ...
const string = ngettext(
'literal for the singular case',
'literal for the plural case',
objectCount
);
``interpolate``
~~~~~~~~~~~~~~~
@ -1083,23 +1086,29 @@ function supports both positional and named interpolation:
corresponding ``fmt`` placeholders in the same order they appear.
For example::
fmts = ngettext('There is %s object. Remaining: %s',
'There are %s objects. Remaining: %s', 11);
s = interpolate(fmts, [11, 20]);
// s is 'There are 11 objects. Remaining: 20'
const formats = ngettext(
'There is %s object. Remaining: %s',
'There are %s objects. Remaining: %s',
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
boolean ``named`` parameter as ``true``. ``obj`` contains a JavaScript
object or associative array. For example::
d = {
count: 10,
total: 50
const data = {
count: 10,
total: 50
};
fmts = ngettext('Total: %(total)s, there is %(count)s object',
'there are %(count)s of a total of %(total)s objects', d.count);
s = interpolate(fmts, d, true);
const formats = ngettext(
'Total: %(total)s, there is %(count)s object',
'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
JavaScript, so the code has to make repeated regular-expression substitutions.