Where to initialize a variable in an AJAX based application


#1

Hello All,

I am getting confused with best practices. I posted a topic earlier but
realized how confusing it was. Thus I will do my best to be as clear as
possible.

I am a bit confused on best practice for instanciate a variable that
will be used accross several methods within the same controller. I need
this because of AJAX, here is a description of what I intend to do:

(all this in done using AJAX in a single page)

  1. User enter postcode -> calls method 1

  2. Method 1 queries a web service (initialization of the wsdl =
    overhead) -> returns a list of addresses for that postcode to the user

  3. User select address -> calls method 2 (same controller as method 1)

  4. Method 2 queries the web service a again to get the details ->
    redisplay the address to the user.

My problem is that I instanciate a variable in method 1 that will deal
with the overhead of scanning the WSDL. When I call method 2, it
reinstanciate that variable thus loosing a couple of seconds. I have the
following:

class PostcodeController < ActionController::Base

def display_address
@p =
PostcodeAnywhereService.new(“http://services.postcodeanywhere.co.uk/uk/lookup.asmx?wsdl”)
res = @p.by_postcode(params[:postcode])
@array_Interim_result = res.byPostcodeResult.results.interimResult
end

def grab_address
@p =
PostcodeAnywhereService.new(“http://services.postcodeanywhere.co.uk/uk/lookup.asmx?wsdl”)
res = @p.fetch_address(params[:pca_id])
@address_result = res.fetchAddressResult.results.address
end
end

I would like to not initialize the @p in the grab_address method. The
value is not kept between display_address and grab_address (if I take
the first line of the grab_address, I end up with a nil value for p)

Is it possible to create a variable within the same controller,
initialize it, and use it along for a same user. I can not get this to
work. Any information is greatly appreciated.

Thanks,
Carl