Skip to content

Commit 24e4109

Browse files
authored
Merge pull request #11 from germanbisurgi/master
Uses the “startval” configuration object to set the JSONEditorr initi…
2 parents dc1dba0 + 6fb7755 commit 24e4109

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/JsonEditorWidget.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,18 @@ public function run()
137137
// Prepare client options
138138
$clientOptions = $this->clientOptions;
139139
$clientOptions['schema'] = $this->schema;
140-
ArrayHelper::remove($clientOptions, 'startval');
140+
141+
try {
142+
$parsedValue = Json::decode($this->value);
143+
} catch (\Exception $e) {
144+
$parsedValue = null;
145+
\Yii::error($e->getMessage(), __METHOD__);
146+
}
147+
148+
if (!empty($parsedValue)) {
149+
$clientOptions['startval'] = $parsedValue;
150+
}
151+
141152
$clientOptions = Json::encode($clientOptions);
142153

143154
// Prepare element IDs
@@ -151,17 +162,6 @@ public function run()
151162
$widgetJs .= "if (!window.jsonEditors) { window.jsonEditors = []; } window.jsonEditors.push(window.{$widgetId});";
152163

153164
$readyFunction = '';
154-
try {
155-
$parsedValue = Json::decode($this->value);
156-
} catch (\Exception $e) {
157-
$parsedValue = null;
158-
}
159-
160-
if (!empty($parsedValue)) {
161-
$encodedValue = Json::encode($parsedValue);
162-
$readyFunction .= "try { {$widgetId}.setValue({$encodedValue}); } catch (e) { console.warn('Could not parse initial value for {$widgetId}, error: '+e); }\n";
163-
}
164-
165165
$readyFunction .= "{$widgetId}.on('change', function() { document.getElementById('{$inputId}').value = JSON.stringify({$widgetId}.getValue()); });\n";
166166

167167
$widgetJs .= "{$widgetId}.on('ready', function() {\n{$readyFunction}\n});";

0 commit comments

Comments
 (0)