jQuery - параметр успеха Ajax как функция объекта - PullRequest
0 голосов
/ 16 ноября 2009

jQuery - параметр успеха ajax я хочу знать, как передать объектный метод (функцию) по параметру успеха функции ajax (извините за мой английский)

Допустим, я уже установил свой ajax.setup со всем, что мне нужно для вызова php на сервере.

function myClass(id)
{
  this.id = id;

  this.showValues=function(xml) 
  {
    alert(this.id); // to prove we r in the right object
    alert(typeof(xml)); // to prove we got the xml     
  };

  this.retrieveValues=function()
  { 
    // wont call the method, this is just text, like writing sucess:"hello"
    $.ajax({success: this.id+'.showValues'}); 


   // calls the method but this.id is undefined (xml is received tho)
    $.ajax({success: this.showValues}); 


    // wont call the method
    $.ajax({success: new Function(this.id+'.showValues')}); 


    // calls the method with the right id but xml is undefined
    $.ajax({success: new Function(this.id+'.showValues()')}); 


    // js error - "xml not defined"
    $.ajax({success: new Function(this.id+'.showValues(xml)')}); 


    // of course, next line works, but i dont wanna define the function here
     $.ajax({success: function() { alert(this.id); alert(typeof(xml)); } });    

  };

}


// even declaring the object as global, so the class has a chance to call a method using the var name
// wich is this.id+'.retrieveValues' ==> object1.retrieveValues
var object1;

function Main() // main =d
{
  object1 = new myClass('object1');
  object1.retrieveValues();

}

еще раз, извините за мой плохой английский .. учусь все еще = d Я надеюсь, что кто-то может мне помочь, спасибо

1 Ответ

1 голос
/ 16 ноября 2009

нашел его с помощью функции API api <_ <мне не хватало параметра "xml" </p>

$.ajax({success: new Function("xml",this.id+'.showValues(xml)')}); 

new Функция ("xml", this.id + '. ShowValues ​​(xml)') похожа на фразу

функция (XML) {object1.showValues ​​(xml); }

спасибо, дедоз. нет проблем. = D

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...