【问题标题】:grails - returning Object or id from formRemotegrails - 从 formRemote 返回 Object 或 id
【发布时间】:2012-11-30 19:54:22
【问题描述】:

我有一个 grails 应用程序,在用户输入新域对象 (Sync) 的名称后,我想保存该对象,移动到同一页面上的片段,并更改 div 的 css 类 (到 js colorbox,如果这很重要)。

为此,我使用锚来设置类并移动到片段并使用 JS 提交 g:formRemote。但是,formRemote 不会返回创建的对象。

gsp 的一部分:

<g:formRemote url="[controller: 'Main', action:'createNewSync']" name="newSyncForm"   >
<g:field type="text" name="newSyncName" />

<a id="ns-link" href="#outline_content" class="outline">
<script>
  $('#ns-link').click(function(){
     $('#newSyncForm').submit();
  });
 </script>
 </g:formRemote>

稍后在 gsp 中,我们要移动以使用内部带有 outline_content 的颜色框。注意需要syncInstance.name。

<script>
$(document).ready(function(){
$(".outline").colorbox({inline:true, width:"1140px", escKey:false, overlayClose:false});
</script>

<div id="sync" class="hidden">
<div id='outline_content' style='padding:10px; background:#fff;' >
    <h2 class="nameheader"><strong style="color:#000;">New Sync:</strong><span class="editable_textile">${syncInstance?.name}</span></h2>
        <div class="number1"><img src="../images/1.png" border="0" /></div>

.....

控制器:

def createNewSync(){
    params.name = params.newSyncName
    def syncInstance =  Sync?.findByName(params.newSyncName) 

if (!syncInstance)
{
        syncInstance = new Sync(params)
        def u = User.findByUsername(springSecurityService.principal)
        syncInstance.properties['createdBy'] = u
        syncInstance.properties['createdDate'] = new Date().toString()
        syncInstance.properties['lastRunTime'] = "Never"
        syncInstance.properties['lastRunOutcome'] = "---"
        syncInstance.properties['isScheduled'] = false
        syncInstance.properties['isComplete'] = false

        syncInstance.save(failOnError: true, flush: true)

    }        
    //doesn't send anything back to page if it's been called remotely
    [syncInstance: syncInstance]
}   

有没有什么方法可以使用该方法获取对创建的对象的引用,以便稍后在页面上使用?如果没有,还有其他方法可以做到这一点吗?

【问题讨论】:

  • 您能否更新您的帖子以获得更多详细信息?您要返回的域类是什么?您希望在页面上显示它的哪一部分以及在哪里显示?同时显示您正在使用的控制器。
  • @JamesKleeh 更新了请求的代码。

标签: javascript forms grails


【解决方案1】:

好的,这就是我要做的事情

1) 为同步创建一个模板。它将是 id 为“sync”的 div 中包含的所有内容,而不是 div 本身。

2) 更新您的 formRemote 标记以更新该 div &lt;g:formRemote update="sync" ... /&gt;

3) 在控制器中渲染模板render(template: "path/to/template", model:[syncInstance: syncInstance])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多