Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I use grails to somehow emit data with anonymous javascript functions
    primarykey
    data
    text
    <p>I would like to have a controller emit a JSON response with something like this:</p> <pre><code>{ sidebar: { xtype: "container", flex: 1, bodyCls: "my-sidebar", itemId: "my-sidebar-container", items: [ { xtype: "panel", animCollapse: false, collapseFirst: false, collapsed: false, collapsible: true, flex: 1, bodyCls: "my-sidebar-message-section", itemId: "my-sidebar-message-section-container", html: "&lt;h1&gt;Messages (2)&lt;/h1&gt;&lt;p&gt;Bank will be closed on September 4th, 2013 for Labor Day&lt;/p&gt;&lt;p&gt;Lorem ipsum dolor sit amet, &lt;a href="http://google.com"&gt;consectetur&lt;/a&gt; adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.&lt;/p&gt;", listeners: { boxready: function(p) { alert('Hi'); } } } ] } } </code></pre> <p>In my Grails controller I define it like this:</p> <pre><code>package com.fourgablesguy.myapp import grails.converters.JSON class SidebarController { def generateSideBar() { def sidebarItems = [ [ xtype: 'panel', animCollapse: false, collapseFirst: false, collapsed: false, collapsible: true, flex: 1, bodyCls: 'my-sidebar-message-section', itemId: 'my-sidebar-message-section-container', html: messageSectionHtml, listeners: [ boxready: "function(p) { alert('Hi');}" ] ] ] [ xtype: 'container', flex: 1, bodyCls: 'my-sidebar', itemId: 'my-sidebar-container', items: sidebarItems ] } </code></pre> <p>However the grails.converters.JSON converts my function() definition to a string so the actual JSON that is emitted by the controller action has a string for the function():</p> <pre><code>{ sidebar: { xtype: "container", flex: 1, bodyCls: "my-sidebar", itemId: "my-sidebar-container", items: [ { xtype: "panel", animCollapse: false, collapseFirst: false, collapsed: false, collapsible: true, flex: 1, bodyCls: "my-sidebar-message-section", itemId: "my-sidebar-message-section-container", html: "&lt;h1&gt;Messages (2)&lt;/h1&gt;&lt;p&gt;Bank will be closed on September 4th, 2013 for Labor Day&lt;/p&gt;&lt;p&gt;Lorem ipsum dolor sit amet, &lt;a href="http://google.com"&gt;consectetur&lt;/a&gt; adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.&lt;/p&gt;", listeners: { boxready: "function(p) {alert('Hi');}" } } } ] } } </code></pre> <p>The specific issue is this line: <strong>boxready: "function(p) {alert('Hi');}"</strong> I would like to know a way to instead return the above as: <strong>boxready: function(p) {alert('Hi');}</strong></p> <p>This is not really "object data" I am trying to return in JSON, I can understand if this is not possible.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload