473,225 Members | 1,340 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

How would I put data from an AJAX call into a custom class instance (w/ Prototype)

Hi All,

I've stumped myself writing an app that uses Prototype and a bit of PHP. Here is what I have:

I have a custom class named Default_county_init_data that, upon initialization makes several Ajax.Request calls to gather data from the server.

What I'm having trouble with is getting the data from the Ajax call back to the custom class instance. I basicially want to get a Javascript array from my PHP page and insert that into the custom class instance once the Ajax call is done.

Here is the most logical way I could think to do it (but it doesn't work):

Expand|Select|Wrap|Line Numbers
  1. var Default_county_init_data = Class.create({
  2. initialize: function(name){
  3. this.name = name;
  5. this.consumables = new Ajax.Request('http://www.stage.emd.wa.gov/dev/get_csv.php?county=' + this.name + '&cat=consumables',
  6. {method: 'get', onSuccess: function(e){
  7. return eval(e.responseText);
  8. }
  9. });
  11. . . .
  13. }
  14. }
The onSuccess handler of the Ajax.Request object evals the response text of the PHP file. I'm hoping it would return that to a property of the class instance (the 'this.consumables = ' bit before the Ajax call), but it doesn't seem to be doing that.

I've also tried setting the value within the onSuccess method-- something like:

this.consumables = eval(e.responseText);

This doesn't seem to work, either, and I'm unclear what 'this' really is at this point in the code (I don't think it's my custom class instance anymore).

Is this an issue with binding (which I've not figured out how to use yet)? I have the Bungee Book which discusses this, it looks like I would want something like this for my onSuccess function:

Expand|Select|Wrap|Line Numbers
  1. new Ajax.Request.bind(this, 'http://www.stage.emd.wa.gov/dev/get_csv.php?county=' + this.name + '&cat=consumables',
  2. {method: 'get', onSuccess: function(e){
  3. this.consumables = eval(e.responseText);
  4. });
But this doesn't seem to work, either . . . doing it this way, it seems that the onSuccess method never gets called!

I would greatly appreciate any suggestions, this app can be viewed at this URL:


Apr 10 '08 #1
3 2039
5,390 Expert Mod 4TB
hi ...

its a problem with the scope ... the callback has its own execution context so you may just use:

Expand|Select|Wrap|Line Numbers
  1. // store a reference to 'this' context of your object
  2. var me = this;
  4. var cb = function(e){
  5.     // clusure the reference here
  6.     me.consumables = eval(e.responseText);
  7. };
  9. new Ajax.Request('http://www.stage.emd.wa.gov/dev/get_csv.php?county=' + this.name + '&cat=consumables', {method: 'get', onSuccess: cb});
kind regards
Apr 11 '08 #2
ok, I can make things work with this I think, thanks!
Apr 11 '08 #3
5,390 Expert Mod 4TB
post back in case you have problems with it ...

kind regards
Apr 12 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Naresh Agarwal | last post by:
Hi I've written a Custom class loader, which reads the bytecode from a database and returns the Class object. For example, bytecode of a class "Test" is stored in the database. Through custom...
by: Graeme Matthew | last post by:
Hi all, quick one, I hope I am explaining this properly, I am wanting to do some introspection on custom object instances, for example: import md5 >>> m = md5.new() >>> type(m) <type...
by: Steve Franks | last post by:
Is there a way I can extend the HttpContext or one of its subclasses to include a property that exposes a custom class of mine to all ASP.NET pages? More specifically, I'd like to use a...
by: Shimon Sim | last post by:
Hi I am working on application that need to hold custom user information - Last and first name, email, some other domain related information. I used to create Base class for all my pages. The base...
by: Corey B | last post by:
Is there a way for an instance of a custom class to access an ASPX page level variable? I know that I can access a Session variable from within a class using the following code: myClassVar =...
by: Zeba | last post by:
Hi guys! I'm new to JS / Ajax; I've been trying to do an Ajax call to my Webservice ( I'm using C# for code-behind). I'm not using any of the libraries available. I am sending my CustID to the...
by: camarkco | last post by:
Why does C# go out of it's way to prevent this? Mark
Claus Mygind
by: Claus Mygind | last post by:
I am having trouble escaping the & in a JSON.stringfy() ajax call. I don't even know if I am stating the problem correctly here. In my app I have linked json2.js from http://www.JSON.org/json2.js...
by: samarinder | last post by:
I am displaying the results by iterating the list in div tag say "results" in my case.For refining of search i am using ajax call. But when i am getting response back from this below snippet ...
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.