You are not logged in.
Pages: 1
Lets start with the bottom - to explain the problem
on our mvc page we use mustache and Javascript - With m1 it works and with m2 not.
we have a mustache partial with this code:
<script>
window.templates = {};
window.templates.vtSettings = '{{VTSettings}}';
</script>
VTSettings has this value : '{myarray:["AB","CD","EF"]}' and is a parameter of our MVC View
IMyWebextensionsApplication = interface(IMVCApplication)
...
procedure MyPage(var VTSettings : Variant);
later in our View we call Javascript to get the Array Elements from myarray:
let VTSetting = replaceAll(window.templates.vtSettings, '{myarray:', '{"myarray":'); // <-- my Workaround :)
let settings = JSON.parse(replaceAll(VTSetting, '"', '"'));
at this place in m1 window.templates.vtSettings has the value
{"myarray":["AB","CD","EF"]}
and in m2
{myarray:["AB","CD","EF"]}
JSON.Parse throws an Exception with the m2 Version. I have to add the quotes.
I debugged to find the reason in m2 and think there is a problem with mormot.core.variants.JsonToAnyVariant and the later ToJSON
Rad Studio 12.1 Santorini
Offline
Try this:
<script>
window.templates = {};
window.templates.vtSettings = '{{ToJson VTSettings}}';
</script>
Offline
Hm - May be you didnt understand what i mean...
How is it possible to add the missing 2 " to the m2 json ?
(i added ToJSON to my mustache line but same result)
@ab i do not understand what you mean with add /json to end of the page URI - What is "end of the page URI" ?
ty
this is generated in the html
window.templates.vtSettings = '{myarray:["AB","CD","EF"]}';
Last edited by itSDS (2022-12-03 09:38:54)
Rad Studio 12.1 Santorini
Offline
(i added ToJSON to my mustache line but same result)
There are predefined Expression Helpers available for Mustache. But you can also write your own helpers. The effort is small. Something like this:
protected
procedure ArticleSizeToTextShort(const pmcValue: Variant; out pmoResult: Variant);
procedure ...ArticleSizeToTextShort(const pmcValue: Variant; out pmoResult: Variant);
const
ARTICLE_SIZE_STR: array[TArticleSize] of String = ('N', 'S', 'L', 'XL', 'XXL');
var
size: Integer;
begin
if VariantToInteger(pmcValue, size) then
pmoResult := ARTICLE_SIZE_STR[TArticleSize(size)]
else
SetVariantNull(pmoResult);
end;
.RegisterExpressionHelpers(['ArticleSizeToTextShort'], [ArticleSizeToTextShort])
With best regards
Thomas
Offline
Ty Thomas, (i didn't recognize this option to write expression Helper before)
But my concern for this ticket is the difference between m1 and m2.
and if this difference is wanted or not.
Rad Studio 12.1 Santorini
Offline
But my concern for this ticket is the difference between m1 and m2.
and if this difference is wanted or not.
I tested your source code in my Mustache Editor, which I described in this article in forum Delphi-PRAXIS.
With the following source code in the editor:
<script>
let VTSetting = '{{Test.myarray}}';
let settings = JSON.parse(VTSetting.replaceAll('"', '"'));
alert(settings[0] + '-' + settings[1] + '-' + settings[2]);
</script>
With this JSON in file Test.json:
{
"myarray": ["AB", "CD", "EF"]
}
The Alert window shows me the expected result "AB-CD-EF".
With best regards
Thomas
Offline
Hi Thomas,
i tried it with your demo, but there is a internal difference to the mvc model i use.
The TDocVariantData in your case has the JSON as String and in mvc its a TDocVariantData which is converted to JSON without quotes - It's not the same...
As conclusion theres a difference passing JSON String to MVC from m1 to m2.
Rad Studio 12.1 Santorini
Offline
Pages: 1