mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
Refs #31493 -- Replaced var with const/let in documentation JS.
This commit is contained in:
parent
30e59705fc
commit
2afa61e7d9
@ -56,13 +56,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();
|
||||
@ -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) {
|
||||
var $ = django.jQuery;
|
||||
const $ = django.jQuery;
|
||||
copyOnClick('.button');
|
||||
assert.equal($('.button').length, 1);
|
||||
$('.button').click();
|
||||
|
@ -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::
|
||||
|
||||
@ -137,7 +137,7 @@ and read the token from the DOM with JavaScript:
|
||||
|
||||
{% csrf_token %}
|
||||
<script>
|
||||
var csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;
|
||||
const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;
|
||||
</script>
|
||||
|
||||
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
|
||||
|
||||
var request = new Request(
|
||||
const request = new Request(
|
||||
/* URL */,
|
||||
{headers: {'X-CSRFToken': csrftoken}}
|
||||
);
|
||||
|
@ -1830,7 +1830,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>&'}``, the output is:
|
||||
|
@ -1080,9 +1080,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``
|
||||
~~~~~~~~~~~~~~~
|
||||
@ -1096,23 +1099,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.
|
||||
|
Loading…
Reference in New Issue
Block a user