2010-07-29 215 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()

解決了你的問題建議更換一個樂。