Mapping multiple table listeners to different object functions?

Hi,

I am trying to encapsulate a “service” class which makes several different http calls.

I wish to handle the return values in different listeners.

But it seems the “event” passed into the listener is always nill (the print in the handlers functions below crashes).

Can anyone please point me in the right direction?

The code:

Service = {}

function Service:new()

      local service = {}

      function service: performRequest1()

             local url1 = “http:/someurl1”

             params = {}

             network.request(url1, “POST”, self.handleRequest1, params)

      end

     

      function service: performRequest2()

             local url2 = “http:/someurl2”

             params={}

             network.request(url2, “POST”, self.handleRequest2, params)

      end

      function service: handleRequest1(event)

             print(event.phase)

      end

      function service: handleRequest2(event)

            print(event.phase)

      end

      return service

end

return FUService

I notice that you define your URLs with a variable “url1”, but when you call network.request(), you pass a variable called “url” (missing the 1 at the end).  “url” is nil, so as a result, it’s possible that network.request() will act strangely, perhaps not passing an event table to the callback.

As a separate matter, you probably want to put local in front of your url1 declarations.

  • Andrew

Thanks!

But those are just typos from of my attempt to simplify my code for the post (post is edited).

The core problem still persists.

In my code, the network request completes successfully and the listenener is called with a nil event.

Got it.

Well I don’t see anything obvious wrong with your code, but perhaps there’s something wrong in your implementation (since you said the above is a simplified version).

To debug further, I’d suggest inserting more print statements, and try running a separate, simplified case.  Forget about the class for now, just run a 5-line program that performs a network request and prints info in the callback.  Then start building back toward your class, and hopefully you’ll run into the spot where something went wrong.

  • Andrew

I notice that you define your URLs with a variable “url1”, but when you call network.request(), you pass a variable called “url” (missing the 1 at the end).  “url” is nil, so as a result, it’s possible that network.request() will act strangely, perhaps not passing an event table to the callback.

As a separate matter, you probably want to put local in front of your url1 declarations.

  • Andrew

Thanks!

But those are just typos from of my attempt to simplify my code for the post (post is edited).

The core problem still persists.

In my code, the network request completes successfully and the listenener is called with a nil event.

Got it.

Well I don’t see anything obvious wrong with your code, but perhaps there’s something wrong in your implementation (since you said the above is a simplified version).

To debug further, I’d suggest inserting more print statements, and try running a separate, simplified case.  Forget about the class for now, just run a 5-line program that performs a network request and prints info in the callback.  Then start building back toward your class, and hopefully you’ll run into the spot where something went wrong.

  • Andrew