Skip to content

Commit 5b13c50

Browse files
committed
0.2.7 release - fixes field names with '-' in them
adds a js_name to the context which has javascript safe names where - is replaced with _ This is needed with inline related forms, as they show up as `relatedname-NUM` which would cause JS to throw an error when - was in a variable name
1 parent 4eac6ad commit 5b13c50

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

django_admin_json_editor/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from .admin import JSONEditorWidget # noqa
22

3-
__version__ = '0.2.6'
3+
__version__ = '0.2.7'

django_admin_json_editor/admin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def render(self, name, value, attrs=None, renderer=None):
5252

5353
context = {
5454
'name': name,
55+
'js_name': name.replace('-','_'),
5556
'schema': schema,
5657
'data': value,
5758
'sceditor': int(self._sceditor),

django_admin_json_editor/templates/django_admin_json_editor/editor.html

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,46 @@
1010
schema: {{ schema|json_dumps }}
1111
};
1212

13-
var {{ name }}_editor;
13+
var {{ js_name }}_editor;
1414

15-
var {{ name }}_changees = 0;
15+
var {{ js_name }}_changees = 0;
1616

17-
function {{ name }}_init(container, options) {
18-
{{ name }}_changees = 0;
17+
function {{ js_name }}_init(container, options) {
18+
{{ js_name }}_changees = 0;
1919
JSONEditor.defaults.options = {{ default_options|json_dumps }};
20-
{{ name }}_editor = new JSONEditor(container, options);
20+
{{ js_name }}_editor = new JSONEditor(container, options);
2121
JSONEditor.plugins.sceditor.emoticonsEnabled = {{ sceditor }};
22-
{{ name }}_editor.on('change', function () {
23-
{{ name }}_changees ++;
24-
var errors = {{ name }}_editor.validate();
22+
{{ js_name }}_editor.on('change', function () {
23+
{{ js_name }}_changees ++;
24+
var errors = {{ js_name }}_editor.validate();
2525
if (errors.length) {
2626
console.log(errors);
2727
}
2828
else {
29-
var json = {{ name }}_editor.getValue();
29+
var json = {{ js_name }}_editor.getValue();
3030
document.getElementById("id_{{ name }}").value = JSON.stringify(json);
3131
}
3232
});
3333
}
3434

35-
{{ name }}_init(container, options);
35+
{{ js_name }}_init(container, options);
3636

3737
{% if data %}
3838
var initial_json = {{ data|safe }};
3939
if (initial_json !== null) {
40-
{{ name }}_editor.setValue(initial_json);
40+
{{ js_name }}_editor.setValue(initial_json);
4141
}
4242
{% endif %}
4343

4444
{% if schema_choice_field_name and schema_choices %}
4545
var schema_choices = {{ schema_choices|json_dumps }};
46-
let ${{ schema_choice_field_name }}_{{ name }} = $('#id_{{ schema_choice_field_name }}');
47-
if ( ${{ schema_choice_field_name }}_{{ name }}.length ) {
48-
$.data(${{ schema_choice_field_name }}_{{ name }}, "current", ${{ schema_choice_field_name }}_{{ name }}.val()); // store the current value
46+
let ${{ schema_choice_field_name }}_{{ js_name }} = $('#id_{{ schema_choice_field_name }}');
47+
if ( ${{ schema_choice_field_name }}_{{ js_name }}.length ) {
48+
$.data(${{ schema_choice_field_name }}_{{ js_name }}, "current", ${{ schema_choice_field_name }}_{{ js_name }}.val()); // store the current value
4949

50-
${{ schema_choice_field_name }}_{{ name }}.change(function(event) {
50+
${{ schema_choice_field_name }}_{{ js_name }}.change(function(event) {
5151
let changeSchema = true;
52-
if ({{ name }}_changees > 1) {
52+
if ({{ js_name }}_changees > 1) {
5353
// only fire this alert if we have made changes.
5454
changeSchema = confirm("Changing '{{ schema_choice_field_name }}' will clear any data in '{{ name }}'. Do you still want to change '{{ schema_choice_field_name }}'?");
5555
}
@@ -63,14 +63,14 @@
6363
iconlib: "fontawesome4",
6464
schema: schemaToSet
6565
};
66-
if ({{ name }}_editor) {
67-
{{ name }}_editor.destroy();
66+
if ({{ js_name }}_editor) {
67+
{{ js_name }}_editor.destroy();
6868
}
69-
{{ name }}_init(container, newOptions);
70-
$.data(${{ schema_choice_field_name }}_{{ name }}, "current", ${{ schema_choice_field_name }}_{{ name }}.val()); // update current value
69+
{{ js_name }}_init(container, newOptions);
70+
$.data(${{ schema_choice_field_name }}_{{ js_name }}, "current", ${{ schema_choice_field_name }}_{{ js_name }}.val()); // update current value
7171
} else {
7272
// undo the category change
73-
${{ schema_choice_field_name }}_{{ name }}.val( $.data(${{ schema_choice_field_name }}_{{ name }}, "current")); // rollback to the old value
73+
${{ schema_choice_field_name }}_{{ js_name }}.val( $.data(${{ schema_choice_field_name }}_{{ js_name }}, "current")); // rollback to the old value
7474
event.preventDefault();
7575

7676
}

0 commit comments

Comments
 (0)