2010-07-29 60 views
1

我已经创建了一个CalendarViewerPortlet自定义对象JS对象。在这个对象中,我存储了诸如portlet的id和上下文路径之类的东西。该对象还有许多自定义方法,其中一些用于获取/设置成员变量,另一些用于执行特定的事情。在jQuery函数里面使用“this”

当我尝试使用“this”引用对象的函数时。在jQuery函数中,它爆炸了。我知道在这种情况下,术语“这个”可能指的是别的东西,但我不确定如何解决问题并让它参照对象,就像我想要的那样。

这里是有问题的代码:

jQuery.ajax({ 
    url: jQuery(formSel).attr("action"), 
    type: "POST", 
    data: jQuery(formSel).serialize(), 
    beforeSend: function(xhr) { 
    jQuery(msgSel).hide(); 
    jQuery(msgSel).html(""); 
    jQuery(tableSel).hide(); 
    jQuery(pagerSel).hide(); 
    jQuery(cpSelector).block({ 
    message: "<img src='"+this.getContextPath()+"/images/icon_loading.gif' align='absmiddle' alt='Loading...' /> Fetching events..." 
    }); 
}, 

注的 “this.getContextPath()”。这是代码失败的地方。我试图引用我的自定义对象的getContextPath()函数。我怎样才能做到这一点?

回答

6

问题是代码this.getContextPath()稍后在一个匿名函数中执行,作为选项传递给ajax()函数,所以你想要的'this'(你自定义的JS对象)在执行该方法时将不可用。可以将它存储在一个变量中并让该函数'关闭'的参考。下面应该工作:

var calendarViewer = this; 

jQuery.ajax({ 
    url: jQuery(formSel).attr("action"), 
    type: "POST", 
    data: jQuery(formSel).serialize(), 
    beforeSend: function(xhr) { 
    jQuery(msgSel).hide(); 
    jQuery(msgSel).html(""); 
    jQuery(tableSel).hide(); 
    jQuery(pagerSel).hide(); 
    jQuery(cpSelector).block({ 
    message: "<img src='"+calendarViewer.getContextPath()+"/images/icon_loading.gif' align='absmiddle' alt='Loading...' /> Fetching events..." 
    }); 
}, 
4

也许尝试使用jQuery(this)

看来我误解你了。在给定的例子中,上下文可能会导致“this”引用jQuery对象。尝试以下技巧:

function myCode() { 
var th = this; 
$.ajax(... , th.getContextPath() ...); 
} 

//好吧,它是否引用jQuery对象。这是因为你在onBeforeSend :(

+0

但这就是我想要的。我希望“this”引用我的自定义对象,而不是任何jQuery。 – Zendog74 2010-07-29 20:30:26

1

你想从getContextPath()方法不使用缩进指向该代码被称为其对象?

快速谷歌搜索只显示它可用到Java,而不是Javascript

+0

我也想知道,在JavaScript或jQuery中没有这样的事情getContextPath。 – Kronass 2010-07-29 20:30:06

+0

对,getContextPath函数在我的自定义对象中。这是我想参考的一个。我所做的任何jQuery函数之外的所有“this”调用都按预期工作。 – Zendog74 2010-07-29 20:32:45

0

,您可以将您的自定义对象,因为这在其它功能通过使用JavaScript中call
如:

function SecondFunc() { 
    alert(this.MyVar); 
} 
function FirstFunc() { 
    var obj = {MyVar:"Test"} 
    SecondFunc.call(obj); 
} 

身体

<input type="button" onclick="FirstFunc()" value="test" /> 

刚在事件中调用FirstFunc(),它将使用您的自定义this执行SecondFunc()。

在你的情况

function raiseSendRequest() 
{ 
sendRequest.call(YourCustomObject); 
} 

function sendRequest() 
{ 

jQuery.ajax({ 
    url: jQuery(formSel).attr("action"), 
    type: "POST", 
    data: jQuery(formSel).serialize(), 
    beforeSend: function(xhr) { 
    jQuery(msgSel).hide(); 
    jQuery(msgSel).html(""); 
    jQuery(tableSel).hide(); 
    jQuery(pagerSel).hide(); 
    jQuery(cpSelector).block({ 
    message: "<img src='"+this.getContextPath()+"/images/icon_loading.gif' align='absmiddle' alt='Loading...' /> Fetching events..." 
    }); 
} 


希望这个作品

0

提示:您应该词法范围熟习的Javascript

如果您有其他物体内部的AJAX设置,那么你可以尝试...

{ 
    //code 
    var ObjectSelf = this; 

    //refer to your object via ObjectSelf 

} 
0

这是一个绑定问题。当您在jQuery方法中调用它时,通常会引用您正在执行您的任务的DOM或HTML元素。

我将在

jQuery.proxy()

解决了你的问题建议更换一个乐。