-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapplication.js
More file actions
91 lines (84 loc) · 1.89 KB
/
application.js
File metadata and controls
91 lines (84 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import Controller from '@ember/controller';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
const DEFAULT_JSON = {
stringKey: 'stringValue',
numberKey: 123,
numArrayKey: [1, 2, 3],
includeNull: [1, null, 'two', false],
mixedKey: ['one', 2.1234, 'three point four five 6', 7.891],
stringArrayKey: [
'one',
'two',
'three',
'stringValue1',
'stringValue2',
'stringValue3',
],
bool: true,
nonBool: false,
objKey: {
foo: 'bar',
baz: 123,
nested: {
more: 'value',
deeper: {
thoughts: [123, 456, 789],
foo: 'bar',
baz: 123,
nested: {
more: 'value',
deeper: {
thoughts: [123, 456, 789],
foo: 'bar',
baz: 123,
nested: {
more: 'value',
deeper: {
thoughts: [123, 456, 789],
stringArrayKey: [
'one',
'two',
'three',
'stringValue',
'stringValue',
'stringValue',
'stringValue',
'stringValue',
],
},
},
},
},
},
},
},
};
function isParseable(json) {
try {
JSON.parse(json);
return true;
} catch {
return false;
}
}
export default class ApplicationController extends Controller {
@tracked
sourceJSONStr = JSON.stringify(DEFAULT_JSON, null, 2);
@tracked
viewJSON = DEFAULT_JSON;
@tracked
isJSONValid = true;
@action
updateJSON(evt) {
this.sourceJSONStr = evt.target.value;
this.isJSONValid = isParseable(this.sourceJSONStr);
if (this.isJSONValid) {
this.viewJSON = JSON.parse(this.sourceJSONStr);
}
}
@action
formatSourceJSONStr() {
this.sourceJSONStr = JSON.stringify(this.viewJSON, null, 2);
}
}