我想让一个控制器发出一个 JSON 响应,如下所示:
{
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: "<h1>Messages (2)</h1><p>Bank will be closed on September 4th, 2013 for Labor Day</p><p>Lorem ipsum dolor sit amet, <a href="http://google.com">consectetur</a> 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.</p>",
listeners: {
boxready: function(p) {
alert('Hi');
}
}
}
]
}
}
在我的 Grails 控制器中,我这样定义它:
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
]
}
但是 grails.converters.JSON 将我的 function() 定义转换为字符串,因此控制器操作发出的实际 JSON 具有 function() 的字符串:
{
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: "<h1>Messages (2)</h1><p>Bank will be closed on September 4th, 2013 for Labor Day</p><p>Lorem ipsum dolor sit amet, <a href="http://google.com">consectetur</a> 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.</p>",
listeners: {
boxready: "function(p) {alert('Hi');}"
}
}
}
]
}
}
具体问题是这一行: boxready:“function(p){alert('Hi');}” 我想知道一种方法来代替上面的返回: boxready:function(p){alert('Hi' );}
这不是我试图以 JSON 格式返回的真正“对象数据”,如果这不可能,我可以理解。