2012-11-30 36 views
0

我有一個grails應用程序,在用戶輸入一個新的域對象(同步)的名稱後,我想保存該對象,移動到同一頁上的一個片段,更改div的css類(如果這很重要,請更改爲js colorbox)。grails - 返回對象或ID從formRemote

爲此,我使用錨來設置類並移動到片段並使用JS提交g:formRemote。但是,formRemote不返回創建的對象。

部分普惠制:

<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] 
} 

有沒有什麼辦法讓到創建的對象的引用稍後使用此方法在頁面上使用?如果沒有,是否有另一種方法來實現這一點?

+0

您可以更新您的文章有更詳細的模板?什麼是您想要返回的域類?你想在頁面上顯示哪部分內容?同時顯示您正在使用的控制器。 –

+0

@JamesKleeh更新了所需的代碼。 –

回答

0

好了,所以這裏是我會做什麼

1)創建同步的模板。這將是div中包含的所有內容,其中id爲「sync」,但不是div本身。

2)更新您的formRemote標籤更新該div <g:formRemote update="sync" ... />

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