莫名其妙地發射數據,我想有一個控制器發出的是這樣的JSON響應:我可以使用的Grails與匿名的JavaScript函數
{
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我的函數()定義轉換爲字符串,使得由控制器的動作發出的實際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');}"
}
}
}
]
}
}
的具體問題是這一行: boxready: 「功能(P){警報( '你好');}」 我想知道的方式,而不是返回上面: boxready:函數(p){alert('Hi');}
這不是真正的「對象數據」我試圖在JSON中返回,我可以理解這是不可能的。
我會建議不要這樣;它可能看起來像上面這些簡單功能的誘人之處,但對於任何稍微複雜的事情來說,它很快就會變成維護噩夢。你試圖解決什麼原始問題? –
@Alexander在Grooveek的答覆中查看我的意見 – fourgablesguy