简体   繁体   English

如何在grails中的.js文件中使用remoteFunction

[英]How to use remoteFunction inside a .js file in grails

I have requirement, that whenever i click on a day(in a fullcalendar) i need to throw a pop up with details. 我有一个要求,就是每当我单击一天(以日历的形式)时,都需要弹出一个带有详细信息的弹出窗口。 I need to trigger an action/controller to get the details. 我需要触发一个动作/控制器来获取详细信息。 I have my event.click in my .js file. 我的event.click在我的.js文件中。 I have been trying to use remoteFunction inside my full calendar jquery. 我一直在尝试在完整日历jquery中使用remoteFunction。 But grails is not recognizing remoteFunction call and my screen is going awry (because of unavailability of .js template). 但是grails无法识别remoteFunction调用,并且我的屏幕出现了问题(因为.js模板不可用)。 Please help me out if possible, 请尽可能帮我

$(calId[calNo]).fullCalendar({
            header : {
                left : ' ',
                center : 'title',
                right : ' '
            },  
            defaultView: 'month',
            selectable: true,  
            weekMode : 'variable',      
            eventColor : 'white',
            editable : false,
            year : eventYr,
            month : calNo,
            events : allocData, 

            dayRender: function (event, element, view) {                        

                for (i = 0, l = holidayData.length; i < l; i++) {
                    var dateString = holidayData[i].substring(0,10);
                    view.element.find('.fc-day[data-date="' + dateString + '"]').css('background-color', '#FF9999');
                    view.element.find('.fc-other-month').css('background-color', '#FFFFFF');                  
                }              
            },
            eventRender: function(event, element, view)
            {
               if(event.start.getMonth() !== view.start.getMonth()) { return false; }
            },
            eventClick: function(event) { 
                  var selectedDate = String(event.start);                 
                  var newData = ${remoteFunction(controller: 'PreSchedule', action: 'calProcess')};
                    alert(newData);
                  $('#dateAllocation #selectedDate').text(String(selectedDate).substring(0,10) + ' ,' + String(selectedDate).substring(28,33));     
                  $('#dateAllocation').modal('show');
            },
            select: function(date) { 
                  var selectedDate = date;                 
                  $('#dateAllocation #selectedDate').text(String(selectedDate).substring(0,10) + ' ,' + String(selectedDate).substring(28,33));     
                  $('#dateAllocation').modal('show');   

            }


        });

May be i am mixing server side code and client side code, messing up with basics. 可能是我在混合服务器端代码和客户端代码,弄混了基础知识。 Thanks in advance. 提前致谢。

Instead of using remotefunction, i tried using jquery.ajax/ createLink. 我没有使用remotefunction,而是尝试使用jquery.ajax / createLink。 But the url is not getting resolved. 但是网址没有得到解析。

  $("#link").click(function(event){
           alert('link');
           event.preventDefault();
           date = '1985-01-01';
           $.ajax({
               url:'${createLink(controller:"Student",action:"checkLink")}',
             //  url:'/checkLink',
                dataType: 'json',
                type: 'POST',
                //data: date,                                       
                success: function() {
                    console.log("The returned data is: ");
                    // show your modal, or do whatever you want.
                }
           });

I can see the error in browser developer tools 我可以在浏览器开发人员工具中看到错误

Failed to load resource: the server responded with a status of 404 (Not Found) 加载资源失败:服务器响应状态为404(未找到)

//smsFrontEnd/student/$%7BcreateLink(controller:%22Student%22,action:%22checkLink%22)%7D // smsFrontEnd / student / $%7BcreateLink(controller:%22Student%22,action:%22checkLink%22)%7D

if possible, please help me out 如果可能的话,请帮帮我

I don't think you would be able to use grails ajax tag libs in .js files. 我认为您无法在.js文件中使用grails ajax标签库。

However, adding to this these taglibs are deprecated and you should not be using them anyway. 但是,不建议您添加这些taglib,并且无论如何都不应使用它们。 As this is not considered to be a good practice 由于这不是一个好习惯

http://grails.org/doc/latest/ref/Tags/remoteFunction.html http://grails.org/doc/latest/ref/Tags/remoteFunction.html

The formFunction tag and other Ajax related tags have been deprecated and will be removed from a future version of Grails. Applications may provide their own Ajax tags and/or Javascript plugins may provide Ajax tags of their own. 

http://en.wikipedia.org/wiki/Unobtrusive_JavaScript http://en.wikipedia.org/wiki/Unobtrusive_JavaScript

I think you are better of using jquery.Ajax instead of remoteFunction there. 我认为您最好使用jquery.Ajax代替那里的remoteFunction。

If your controller is returning JSON data you can fetch and use it as follows: 如果您的控制器正在返回JSON数据,则可以按以下方式获取和使用它:

Instead of: 代替:

var newData = ${remoteFunction(controller: 'PreSchedule', action: 'calProcess')};
alert(newData);

Use the jQuery $.ajax method: 使用jQuery $.ajax方法:

$.ajax({
    dataType: 'json',
    url: '${createLink(controller: 'preSechedule', action: 'callProcess')}',
    data: {}, // no parameters
    success: function(data) {
        window.alert("The returned data is: "+data);
        // show your modal, or do whatever you want.
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM