开发者

Using "this" inside of jQuery functions

I have created a CalendarViewerPortlet custom object JS object. In this object, I am storing things like the port开发者_高级运维let's id and its context path. The object also has many custom methods, some to get/set the member variables and some to do specific things.

When I try to reference a function of the object using "this." inside of a jQuery function, it blows up. I get that the term "this" in that context is probably referring to something else, but I am not sure how to get around the problem and have it reference the object, as I want it to.

Here is the offending code:

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..."
  });
},

NOTE the "this.getContextPath()". That is where the code is failing. I am trying to reference the getContextPath() function of my custom object. How can I do that?


The problem is the code this.getContextPath() is executed later in an anonymous function passed as on option to ajax() function. So the 'this'(your custom JS object) you want will not be available at the point of the execution of the method. You can store it in a variable and let the function 'closure' the reference. The following should work:

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..."
  });
},


maybe try using jQuery(this)

it seems I've misunderstood you. In the given example, the context may cause "this" refer to jQuery object. Try the following trick:

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

// well, it DOES refer jQuery object. it's because you don't use indentation to point that the code is called in onBeforeSend :(


You want the getContextPath() method from which object?

A quick Google search only shows it to be available to Java, not Javascript.


You can send your custom object as this in the other function by using call in javascript
eg:

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

in body

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

just call FirstFunc() in the event and it will execute the SecondFunc() with your custom this.

In your case

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..."
  });
}


Hope this works


Tip: You should get familiarized with Lexical Scoping in Javascript

If you have the ajax setup inside another object then you could try...

{
  //code
  var ObjectSelf = this;

  //refer to your object via ObjectSelf

}


This is a binding problem. When you call this inside a jQuery method it is usually referenced to the DOM- or HTML-Element you are performing your task on.

I would recomend a lok at

jQuery.proxy()

which solves your problems.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜