This is all working well. However, I want to create a JS object that does this internally. Essentially, I am creating the JS object, declaring 'request' as a property of the object, declaring process_it() as a method
of the object, and using 'this.request' instead of 'request' thruought.
But it doesn't work. I get
I'm using XMLHttpRequest to generate a request object, and fetch some
data from the server.
--snip-- <
This is all working well. However, I want to create a JS object that does this internally. Essentially, I am creating the JS object, declaring 'request' as a property of the object, declaring process_it() as a method
of the object, and using 'this.request' instead of 'request' thruought.
But it doesn't work. I get
this.request has no properties
when I try to execute the if() statements at the top of process_it().
Any ideas?
I'd need to see the code that's not working to help more.
I would honestly suggest looking into something like prototype.js or jquery, which does a lot of handling for encapsulation... "this" in the context of the handler will refer to the request object itself... you will want to exte off of the request object, something like request.handler = this, when creating the request object, you can then refer back through request.handler.something etc...
I believe he's having trouble with the parent reference, in the referenced function "this" refers directly to the request object, not the containing object,
you can work around this, by creating a back-reference to the
containing object request.parent = this; for example... there's a lot
of mentions of similar issues out there.
In the context of the handler, "this.parent" is undefined. "request" is a global, and the whole point is to get away from having to use a global, because using a global means I can only safely instantiate the class ONCE
if I am to avoid the various class instances clobbering the shared/global request object.
see live at.. http://www.theroughnecks.net/temp/
The following sample works, note: I am including prototype.js before this script gets called... data.txt is a simple text file, with a short "done no text in it... Event.observe, and Ajax.Request are both part of the prototype.js framework script... it's *REALLY* useful. http://prototypejs.or
see live at.. http://www.theroughnecks.net/temp/
The following sample works, note: I am including prototype.js before this
script gets called... data.txt is a simple text file, with a short "done no >> text in it... Event.observe, and Ajax.Request are both part of the
prototype.js framework script... it's *REALLY* useful. http://prototypejs.or
I had hoped there was a way to use the festure withOUT including 3,200
lines of someone elses code. :-(
I just referenced prototype because there is a lot of value there. between $A().each(function() {}); and a few other niceties, it makes it well worth t inclusion...
I just referenced prototype because there is a lot of value there. between >> $A().each(function() {}); and a few other niceties, it makes it well worth t >> inclusion...
The JSON support is good. I just seem to be having trouble using the
object properties and methods from inside the handler function.
The JSON support is good. I just seem to be having trouble using the object properties and methods from inside the handler function.
instantiate private variables, as in my example, then address them
without "this"
instantiate private variables, as in my example, then address them
without "this"
from inside the public api, or other methods, the private variables are available without "this."
The JSON support is good. I just seem to be having trouble using the
object properties and methods from inside the handler function.
instantiate private variables, as in my example, then address them
without "this"
I was declaring my methods external to the constructor like so:
function Menagerie_get() { . . . }
function Menagerie_put() { . . . }
function Menagerie() {
. . .
// public methods
this.get = Menagerie_get;
this.put = Menagerie_put;
}
which is ugly, but that's the way the legacy code i am working with was originally written. I couldn't get them to access private variables
without using "this". But the response handler function couldn't access private variables with 'this' either.
Ok, it's working after an ugly fashion, and when I get a chance I'll move all the method seclarationss into the constructor and see if I can get it
to be less ugly.
instantiate private variables, as in my example, then address them
without "this"
from inside the public api, or other methods, the private variables are
available without "this."
But apparently, the public *methods* are not available from inside the response handler. Let me expand your example code a bit:
function Menagerie() {
//private variable
var a_list = new Array();
//private method - add animal to localized array
function add_animal(animal_name) {
a_list[a_list.length] = animal_name;
}
//private callback function.
function populate_callback(jsonResponse) {
for (var i=0; i<jsonResponse.animals.length; i++)
add_animal(jsonResponse.animals[i].name);
}
//public api
return {
getList:function() { return a_list; },
say_hi:function() { alert( "Hi!" );}, // <------------- silly method
populate:function() {
say_hi(); // <-------------------------------------- works fine
new Ajax.Request(
'/json_animals.php', {
method: 'get',
onSuccess: function(request) {
...
}
}
);
}
}
}
Obviously, for any non-trivial object, you are almost certainly going to need to access the methods defined elsewhere in the object. Yes, you can replicate the code from the body of the methods you need like so:
populate:function() {
new Ajax.Request(
'/json_animals.php', {
method: 'get',
onSuccess: function(request) {
var json = request.responseText.evalJSON();
for (i=0; i<json.animals.length; i++) {
a_list[a_list.length] = json.animals[i].name; // <-- dup
}
}
}
);
}
but obviously, as the object and it's methods become complex, you are duplicating more and more code.
first, don't use a literal function, use a reference to the object's method defined privately.... second, if you want error checking, have a add_animal(name) do the checking internally in a separate method.
first, don't use a literal function, use a reference to the object's method >> defined privately.... second, if you want error checking, have a
add_animal(name) do the checking internally in a separate method.
Hmmm... I tried something close to that. I used a reference to a method thaty was defined as a part of the public API (*not* a private method) and it choked.
Like many languages that had OOP added as an afterthought, JS's support
for object is a little rough around the edges.
On a slightly different note, I've been having some trouble generating
good JSON data to return. If I insert a single \n in the output the response won't evaluate. I have to do this:
Like many languages that had OOP added as an afterthought, JS's
support for object is a little rough around the edges.
very much so.. it's kind of nice the structure doesn't create too much variance, but it's also kind of difficult to think in those terms... no class/public/private declaration syntax.. etc.
Sysop: | MCMLXXIX |
---|---|
Location: | Prospect, CT |
Users: | 325 |
Nodes: | 10 (0 / 10) |
Uptime: | 136:51:20 |
Calls: | 501 |
Messages: | 218442 |