1
Vote

@Display.TokenHint() from xxxxSettings.cshtml declares each time the same javascript variable

description

As my ContentItem was using several parts with settings,
I was surprised to discovered in the html generated in Content Model editing this
<meta content="Orchard" name="generator" />
<meta content="IE=edge,chrome=1" name="X-UA-Compatible" />
<link href="/modules/orchard.themes/Content/orchard.ico" rel="shortcut icon" type="image/x-icon" />
<script type="text/javascript">
//<![CDATA[
    var tokensUrl = '/Admin/Tokens/Tokens';
//]]>
</script>

<script type="text/javascript">
//<![CDATA[
    var tokensUrl = '/Admin/Tokens/Tokens';
//]]>
</script>

<script type="text/javascript">
//<![CDATA[
    var tokensUrl = '/Admin/Tokens/Tokens';
//]]>
</script>

There is certainly a way to avoid declaring tokensUrl as many time as cshtml models are included ?

comments

Piedone wrote Sep 17, 2013 at 10:44 PM

If this wouldn't be in the core I'd suggest WasNotDisplayed(), but something similar could be employed.

CSADNT wrote Sep 17, 2013 at 11:14 PM

But it is in Orchard core....

BertrandLeRoy wrote Sep 17, 2013 at 11:53 PM

Christian is right. The right thing to do here is to make sure the script is only displayed once, as it's not changing from one instance to the next.

Piedone wrote Sep 18, 2013 at 9:50 AM

@CSADNT: yes, that's why I said that something similar could be employed. The implementation of the method is as following:
        public static bool WasNotDisplayed(this WebViewPage page, string key)
        {
            var workContext = page.ViewContext.RequestContext.GetWorkContext();
            if (workContext.GetState<string>(key) == null)
            {
                workContext.SetState(key, "displayed");
                return true;
            }
            return false;
        }
This can work as a simple guard.

CSADNT wrote Sep 18, 2013 at 7:33 PM

Interesting trik, thanks