views:

168

answers:

1

Hi there,

for past two hours i've been trying to pass responseText to the object i'm calling Ajax.Request in. And it just isn't happening :) Here's the code;

    var traziSuggest = Class.create({  
   initialize:function( elemenat, sugDiv )
   {
    this.elemenat = $( elemenat );
    this.elemenat.onkeyup = this.uzmiSug.bindAsEventListener(this);
    this.elemenat.onkeydown = this.uzmiSug.bindAsEventListener(this);

    this.rezultat = new Array();

    this.sugDiv = $( sugDiv );

    this.prethodnaDuzina = this.elemenat.value.length;
   },
   uzmiSug:function()
   {
    this.novaDuzina = this.elemenat.value.length;

    if ( this.novaDuzina > 3 && this.novaDuzina != this.prethodnaDuzina )
    {     
     new Ajax.Request( '/ajax/traziSuggest.php?sta=mjesto&q=' + this.elemenat.value , 
     {
      method: 'get',
      onSuccess: function(transport)
      {
       traziSuggest.rezultat = transport.responseText;
      }
     });

     this.prikaziSug;
    }
    else
    {
     this.sugDiv.hide();
    }

    this.prethodnaDuzina = this.novaDuzina;
   },
   prikaziSug: function()
   {
    var zemlje = this.rezultat.zemlje;
    var mjesta = this.rezultat.mjesta;
    var lokacije = this.rezultat.lokacije;
   }
  });

So, what i need is to pass the transport.responseText to the objects this.rezultat variable. Any suggestions ?

A: 

You can create a closure, like this:

if ( this.novaDuzina > 3 && this.novaDuzina != this.prethodnaDuzina )
{   

  var self = this; 

 new Ajax.Request( '/ajax/traziSuggest.php?sta=mjesto&q=' + this.elemenat.value , 
 {
  method: 'get',
  onSuccess: function(transport)
  {
   self.rezultat = transport.responseText;
  }
 });
Greg