raw method in <head> does not work

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

raw method in <head> does not work

tpaulsme
I tested out the raw() method and found that it works in the <body> but not in the <head>.

Has anyone else noticed this?

Anyone know of a workaround, besides something like this - which works:

<r:script disposition="head">
${raw(new org.codehaus.groovy.grails.web.pages.FastStringWriter().append(headerJs).buffer)}
</r:script>

Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: raw method in <head> does not work

SAgrawal14
Why do you want to write raw data to your head section programatically...???


On Sat, Dec 21, 2013 at 5:43 AM, tpaulsme <[hidden email]> wrote:
I tested out the raw() method and found that it works in the <body> but not
in the <head>.

Has anyone else noticed this?

Anyone know of a workaround, besides something like this - which works:

<r:script disposition="head">
${raw(new
org.codehaus.groovy.grails.web.pages.FastStringWriter().append(headerJs).buffer)}
</r:script>

Thanks!




--
View this message in context: http://grails.1312388.n4.nabble.com/raw-method-in-head-does-not-work-tp4652737.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Thank You,

SA
Reply | Threaded
Open this post in threaded view
|

Re: raw method in <head> does not work

Daniel Woods
Why do you want to write raw data to your head section programatically...???

I think it's best to not bring questions like this to the mailing list. It's ok to think that in your head, but we really don't need to know the reason in order to provide support (esp cuz he didn't ask for an alternative), and it comes across as condescending and rude. Let's keep these conversations support-oriented and as helpful as possible.

Dan
t: @danveloper
 


On Sat, Dec 21, 2013 at 5:43 AM, tpaulsme <<a href="javascript:_e({}, &#39;cvml&#39;, &#39;tpaulsme@gmail.com&#39;);" target="_blank">tpaulsme@...> wrote:
I tested out the raw() method and found that it works in the <body> but not
in the <head>.

Has anyone else noticed this?

Anyone know of a workaround, besides something like this - which works:

<r:script disposition="head">
${raw(new
org.codehaus.groovy.grails.web.pages.FastStringWriter().append(headerJs).buffer)}
</r:script>

Thanks!




--
View this message in context: http://grails.1312388.n4.nabble.com/raw-method-in-head-does-not-work-tp4652737.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: raw method in <head> does not work

Jeff Scott Brown-2
I disagree. I think it was a perfectly reasonable question. Knowing why people do things often helps us improve the framework to better support those scenarios and often leads to a suggestion of a better way to do whatever appears problematic. 



JSB

Sent from my iPhone

On Dec 30, 2013, at 10:07 AM, Daniel Woods <[hidden email]> wrote:

Why do you want to write raw data to your head section programatically...???

I think it's best to not bring questions like this to the mailing list. It's ok to think that in your head, but we really don't need to know the reason in order to provide support (esp cuz he didn't ask for an alternative), and it comes across as condescending and rude. Let's keep these conversations support-oriented and as helpful as possible.

Dan
t: @danveloper
 


On Sat, Dec 21, 2013 at 5:43 AM, tpaulsme <<a href="javascript:_e({}, 'cvml', 'tpaulsme@gmail.com');" target="_blank">tpaulsme@...> wrote:
I tested out the raw() method and found that it works in the <body> but not
in the <head>.

Has anyone else noticed this?

Anyone know of a workaround, besides something like this - which works:

<r:script disposition="head">
${raw(new
org.codehaus.groovy.grails.web.pages.FastStringWriter().append(headerJs).buffer)}
</r:script>

Thanks!




--
View this message in context: http://grails.1312388.n4.nabble.com/raw-method-in-head-does-not-work-tp4652737.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: raw method in <head> does not work

tpaulsme
This post was updated on .
In reply to this post by SAgrawal14
[no offense taken on the "why do this?" question.  IMHO - it's OK to ask why, particularly if it could help provide the best answer.  And, I've read that it's best to keep dynamic JS injections into the <head> to a minimum, but this is a specific somewhat non-standard case.]

[Another note: this is a factor when the following setting is in place in the file Config.groovy
grails.views.default.codec = "html"]

Reason for dynamic code in <head>:

I'm building a Single Page App (SPA) to serve to a browser. The browser is on a touch-screen and in a public place where many people will use it.  Each customer will purchase different components. With a http request the selected components are then dynamically assembled server-side and presented based upon the encoded customer ID passed in a URL parameter. Along with each component will be custom data and settings for each customer stored in the Database.  These settings must be grabbed and put into the SPA, too.

The components will mostly be jQuery plugins, and these will be in logically separated Files (since they will only change with maintenance and updates) and included via the Resources plugin.  However, the javascript that calls the plugins and needs settings and parameters may either need to be custom, or only included if the customer has purchased that component. AND the settings and paraemeters, etc. are in the DB for that customer.  Thus, the DB must be accessed, then the json (containing customer settings for the component) and javascript created - and my idea was to include it with the Resources <r:script> tag.

I looked at the Resources Creating Custom Mappers, but that seems to drive the "visibility" of accessing the custom settings from the DB too far down in the layers -which would make it hard for future maintainers.

The way I'm proposing to use it would have my Service access the DB and setting up the custom JS and plugging it into the <head> (or <body>  [note that same issue exists if putting JS into the <body> inside the <r:script> tag using the raw() method].  This seems more intuitive and Grails-likce (have the Services access the DB and send in the results via a tag) for operations and maintenance, than having a Resource Custom Mapper pre-process and access a Service.

So, if there are no workarounds or other suggestions, I'll just stick with the workaround on my original Dec 20, 2013; 5:13pm post.


Thanks!


Reply | Threaded
Open this post in threaded view
|

Re: raw method in <head> does not work

Roberto Guerra
I don’t think grails is a good fit for SPA. Although we have built a couple of SPA apps for grails, it is not ideal for many reasons:

1. Hard to test your front end code. And no, using geb and selenium doesn’t count. If you do TDD, you are screwed.
2. You can not iterate fast enough. A change to your SPA app means you have to redeploy your entire app. It is much faster and convenient to deploy your front end independent of your back end. That way both can iterate at their own pace.
3. If you have a team of designers working with you, getting them to run grails on their machine, and set up the database and all that heavy stuff might be too much. It is much easier to configure a grunt build that will boot up the app on expressjs for development that talks to an instance of the app running on test server. Your designers will thank you.
4. Same goes for your front end/javascript team.
5. The resource plugin is not ideal for SPA. Too much friction. And if you want to use AMD or CommonJs, you are screwed.

 


On 30-Dec-2013, at 11:50 AM, tpaulsme <[hidden email]> wrote:

> [no offense taken on the "why do this?" question.  IMHO - it's OK to ask why,
> particularly if it could help provide the best answer.  And, I've read that
> it's best to keep dynamic JS injections into the <head> to a minimum, but
> this is a specific somewhat non-standard case.]
>
> Reason for dynamic code in <head>:
>
> I'm building a Single Page App (SPA) to serve to a browser. The browser is
> on a touch-screen and in a public place where many people will use it.  Each
> customer will purchase different components. With a http request the
> selected components are then dynamically assembled server-side and presented
> based upon the encoded customer ID passed in a URL parameter. Along with
> each component will be custom data and settings for each customer stored in
> the Database.  These settings must be grabbed and put into the SPA, too.
>
> The components will mostly be jQuery plugins, and these will be in logically
> separated Files (since they will only change with maintenance and updates)
> and included via the Resources plugin.  However, the javascript that calls
> the plugins and needs settings and parameters may either need to be custom,
> or only included if the customer has purchased that component. AND the
> settings and paraemeters, etc. are in the DB for that customer.  Thus, the
> DB must be accessed, then the json (containing customer settings for the
> component) and javascript created - and my idea was to include it with the
> Resources <r:script> tag.
>
> I looked at the Resources Creating Custom Mappers, but that seems to drive
> the "visibility" of accessing the custom settings from the DB too far down
> in the layers -which would make it hard for future maintainers.
>
> The way I'm proposing to use it would have my Service access the DB and
> setting up the custom JS and plugging it into the <head> (or <body>  [note
> that same issue exists if putting JS into the <body> using the raw()
> method].  This seems more intuitive and Grails-likce (have the Services
> access the DB and send in the results via a tag) for operations and
> maintenance, than having a Resource Custom Mapper pre-process and access a
> Service.
>
> So, if there are no workarounds or other suggestions, I'll just stick with
> the workaround on my original Dec 20, 2013; 5:13pm post.
>
>
> Thanks!
>
>
>
>
>
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/raw-method-in-head-does-not-work-tp4652737p4652897.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: raw method in <head> does not work

tpaulsme
Hi Roberto,

Thanks for your input.  We are too far down the infrastructure to back out of Grails, now for this SPA.  

Since you have deployed a couple of SPAs with grails, do you care to provide any hints on ways to make it easier?

Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: raw method in <head> does not work

Roberto Guerra
I’ve never had to do what you are trying to do. Your use case is a little complicated. Have you looked at the asset-pipeline plugin?  You might be able to programatically set the js dependencies with that. The asset pipeline is pretty good, and the core developer is very helpful.


On 30-Dec-2013, at 1:21 PM, tpaulsme <[hidden email]> wrote:

> Hi Roberto,
>
> Thanks for your input.  We are too far down the infrastructure to back out
> of Grails, now for this SPA.  
>
> Since you have deployed a couple of SPAs with grails, do you care to provide
> any hints on ways to make it easier?
>
> Thanks!
>
>
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/raw-method-in-head-does-not-work-tp4652737p4652901.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: raw method in <head> does not work

SAgrawal14
This post was updated on .
Hey tpaulsme,

Thanks for your descriptive response.

Here is something you can try out:

Create a custom tag say *xyz* & render your dynamic javascript code from
their to the *head* of the page.

In the tag *xyz* either you can set default encoding like: *static
encodingForTags = [ xyz: "raw" ]* or you could write somethings like this:

out.println '<script type="text/javascript">'
    withCodex("JavaScript") {
        out << "your-content"
    }
    out.println()
    out.println '<script>'


Thanks,

Shashank
Thank You,

SA
Reply | Threaded
Open this post in threaded view
|

Re: raw method in <head> does not work

SAgrawal14
Sorry, Forgot to ask, which version of grails you are using. Above answer will run on grails version 2.3.x

Shashank


On Tue, Dec 31, 2013 at 1:21 PM, Shashank Agrawal <[hidden email]> wrote:
Hey tpaulsme,

Thanks for your descriptive response.

Here is something you can try out:

Create a custom tag say xyz & render your dynamic javascript code from their to the head of the page.

In the tag xyz either you can set default encoding like: static encodingForTags = [ xyz: "raw" ] or you could write somethings like this:

out.println '<script type="text/javascript">'
    withCodex("JavaScript") {
        out << "your-content"
    }
    out.println()
    out.println '<script>'

Thanks,

Shashank


On Tue, Dec 31, 2013 at 12:57 AM, Roberto Guerra <[hidden email]> wrote:
I’ve never had to do what you are trying to do. Your use case is a little complicated. Have you looked at the asset-pipeline plugin?  You might be able to programatically set the js dependencies with that. The asset pipeline is pretty good, and the core developer is very helpful.


On 30-Dec-2013, at 1:21 PM, tpaulsme <[hidden email]> wrote:

> Hi Roberto,
>
> Thanks for your input.  We are too far down the infrastructure to back out
> of Grails, now for this SPA.
>
> Since you have deployed a couple of SPAs with grails, do you care to provide
> any hints on ways to make it easier?
>
> Thanks!
>
>
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/raw-method-in-head-does-not-work-tp4652737p4652901.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email




Thank You,

SA
Reply | Threaded
Open this post in threaded view
|

Re: raw method in <head> does not work

Lari Hotari -
In reply to this post by tpaulsme

What Grails version are you using? This bug was fixed recently in 2.3.4. http://jira.grails.org/browse/GRAILS-10839

Lari

21.12.2013 2.14 kirjoitti "tpaulsme" <[hidden email]>:
I tested out the raw() method and found that it works in the <body> but not
in the <head>.

Has anyone else noticed this?

Anyone know of a workaround, besides something like this - which works:

<r:script disposition="head">
${raw(new
org.codehaus.groovy.grails.web.pages.FastStringWriter().append(headerJs).buffer)}
</r:script>

Thanks!




--
View this message in context: http://grails.1312388.n4.nabble.com/raw-method-in-head-does-not-work-tp4652737.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: raw method in <head> does not work

tpaulsme
Which Grails version?

When I rt-click on the project name, Properties, Grails, Grails Installation: Grails 2.3.4

When I run this in my GSP:

<r:style>${headerCss}</r:style>
                        <r:script disposition="head">${raw(new org.codehaus.groovy.grails.web.pages.FastStringWriter().append(headerJs).buffer)}</r:script>
                        <script type="javascript">${raw(headerJs)}</script>
I get this rendered HTML for the last one above:

<script type="javascript">alert(&#39;HEADER for &#39;+[null]);</script>
and this for the Css and the workaround...

<script type="text/javascript">alert('HEADER for '+[null]);</script><style type="text/css">/* this is for [[null]] */ p {color: red !important;}</style>
NOTE: This is my Config.groovy....
========================
// Legacy setting for codec used to encode data with ${}
grails.views.default.codec = "html" // none, html, base64

// The default scope for controllers. May be prototype, session or singleton.
// If unspecified, controllers are prototype scoped.
grails.controllers.defaultScope = 'singleton'

// GSP settings
grails {
    views {
        gsp {
            encoding = 'UTF-8'
            htmlcodec = 'xml' // use xml escaping instead of HTML4 escaping
            codecs {
                expression = 'html' // escapes values inside ${}
                scriptlet = 'html' // escapes output from scriptlets in GSPs
                taglib = 'none' // escapes output from taglibs
                staticparts = 'none' // escapes output from static template parts
            }
        }
        // escapes all not-encoded output at final stage of outputting
        filteringCodecForContentType {
            //'text/html' = 'html'
        }
    }
}
========================

When I change the encodings above from 'html' to 'none', I get working javascript.

CONCLUSION: The fix works only for the "none" encoding setting in 2.3.4.

Thanks,

Todd