---
title: "JavaScript"
version: 1.9
locale: id
source: https://docs.djangoproject.com/id/1.9/internals/contributing/writing-code/javascript/
canonical: https://djangodocs.dev/id/1.9/internals/contributing/writing-code/javascript/
---
# JavaScript

Selagi kebanyakan inti Django adalah Python, aplikasi penyumbang `admin` and `gis` mengandung kode JavaScript.

Silahkan mengikuti standar pengkodean ketika menulis kode javaScript untuk penyertaan di Django.

## Gaya kode

- Please conform to the indentation style dictated in the `.editorconfig`
  file. We recommend using a text editor with [EditorConfig](http://editorconfig.org/) support to avoid
  indentation and whitespace issues. Most of the JavaScript files use 4 spaces
  for indentation, but there are some exceptions.
- When naming variables, use `camelCase` instead of `underscore_case`.
  Different JavaScript files sometimes use a different code style. Please try to
  conform to the code style of each file.
- Use the [JSHint](http://jshint.com/) code linter to check your code for bugs and style errors.
  JSHint will be run when you run the JavaScript tests. We also recommended
  installing a JSHint plugin in your text editor.

## Tambalan JavaScript

Django's admin system leverages the jQuery framework to increase the
capabilities of the admin interface. In conjunction, there is an emphasis on
admin JavaScript performance and minimizing overall admin media file size.
Serving compressed or "minified" versions of JavaScript files is considered
best practice in this regard.

To that end, patches for JavaScript files should include both the original
code for future development (e.g. `foo.js`), and a compressed version for
production use (e.g. `foo.min.js`). Any links to the file in the codebase
should point to the compressed version.

### Memadatkan JavaScript

Untuk menyederhanakan pengolahan menyediakan kode JavaScript yang dioptimalkan, Django menyertakan tulisan Python berguna yang harus digunakan untuk membuat versi "dipekercil". Untuk menjalankannya:

```console
$ pip install closure
$ python django/contrib/admin/bin/compress.py
```

Behind the scenes, `compress.py` is a front-end for Google's
[Closure Compiler](https://developers.google.com/closure/compiler/) which is written in Java. The Closure Compiler library is
not bundled with Django, but you can install it using pip as done above. The
Closure Compiler library requires [Java](https://www.java.com) 7 or higher.

Please don't forget to run `compress.py` and include the `diff` of the
minified scripts when submitting patches for Django's JavaScript.

## Percobaan JavaScript

Django's JavaScript tests can be run in a browser or from the command line.
The tests are located in a top level `js_tests` directory.

### Menulis percobaan

Percobaan JavasScript Django menggunakan [QUnit](https://qunitjs.com/). Ini adalah sebuah contoh percobaan modul:

```javascript
module('magicTricks', {
    beforeEach: function() {
        var $ = django.jQuery;
        $('#qunit-fixture').append('<button class="button"></button>');
    }
});

test('removeOnClick removes button on click', function(assert) {
    var $ = django.jQuery;
    removeOnClick('.button');
    assert.equal($('.button').length === 1);
    $('.button').click();
    assert.equal($('.button').length === 0);
});

test('copyOnClick adds button on click', function(assert) {
    var $ = django.jQuery;
    copyOnClick('.button');
    assert.equal($('.button').length === 1);
    $('.button').click();
    assert.equal($('.button').length === 2);
});
```

Silahkan rundingkan dokumentasi QUnit untuk informasi pada jenis-jenis dari [assertions supported by QUnit](https://api.qunitjs.com/category/assert/).

### Menjalankan percobaan

Percobaan JavaScript dapat berjalan dari perambah jaringan atau dari baris perintah.

#### Dicoba dari sebuah perambah jaringan

Untuk menjalankan percobaan dari perambah jaringan, buka `js_tests/tests.html` di perambah anda.

Untuk mengukur cakupan kode ketika menjalankan percobaan, anda butuh melihat berkas itu melalui HTTP. Untuk melihat cakupan kode:

- Jalankan `python -m http.server` (atau `python -m SimpleHTTPServer` pada Python 2) dari direktori akar (bukan dari dalam `js_tests`).
- Buka [http://localhost:8000/js\_tests/tests.html](http://localhost:8000/js_tests/tests.html) di perambah jaringan anda.

#### Percobaan dari baris perintah

Untuk menjalankan percobaan dari baris perintah, anda butuh mempunyai [Node.js](https://nodejs.org/) terpasang.

Setelah memasang Node.js, pasang ketergantungan percobaan JavaScript dengan menjalankan berikut dari akar dari pemeriksaan Django:

```console
$ npm install
```

lalu jalankan percobaan dengan:

```console
$ npm test
```
