Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
dizzywhip
Dec 23, 2005

What are you expecting it to be? The Ajax function? The 'this' keyword refers to the object that contains the function, not the function itself. If you're defining the Ajax function in the global scope then 'this' will refer to the window object.

You can try just passing in Ajax instead of AJAX, I suppose, though it would probably be better to rethink how you're organizing your code. It's best to avoid working in the global scope entirely. You could try putting your function into your own object, so you would then be able to access it via this.Ajax.

Adbot
ADBOT LOVES YOU

Fruit Smoothies
Mar 28, 2004

The bat with a ZING

Gordon Cole posted:

What are you expecting it to be? The Ajax function? The 'this' keyword refers to the object that contains the function, not the function itself. If you're defining the Ajax function in the global scope then 'this' will refer to the window object.

You can try just passing in Ajax instead of AJAX, I suppose, though it would probably be better to rethink how you're organizing your code. It's best to avoid working in the global scope entirely. You could try putting your function into your own object, so you would then be able to access it via this.Ajax.

If it was any other object than "Ajax" I could probably re-organise it into my other classes. The trouble is, every ajax request is called this way throughout the code, and sometimes the response function needs to call Ajax().request() which effectively submits the request again.

Any other ideas?

dizzywhip
Dec 23, 2005

I'm having a hard time understanding what you're trying to do here and what the problem is. It looks like you're defining the Ajax function in the global scope (on the window object), meaning anything in your program can already access it, but you're also trying to pass it into a callback function. If they can already access the function globally why do they need to get it passed back to them?

Why are you wrapping jQuery's ajax function in the first place?

Fruit Smoothies
Mar 28, 2004

The bat with a ZING

Gordon Cole posted:

I'm having a hard time understanding what you're trying to do here and what the problem is. It looks like you're defining the Ajax function in the global scope (on the window object), meaning anything in your program can already access it, but you're also trying to pass it into a callback function. If they can already access the function globally why do they need to get it passed back to them?

Why are you wrapping jQuery's ajax function in the first place?

There are multiple instances of the object. I'm wrapping it because it contains a lot of error handling code that goes beyond simply assigning a standard error handling function; and it allows me to more easily load multiple URLs and keep track of them etc.

There are also many instances where the Ajax object is created within other classes. The issue is that I need my response function to be able to access the Ajax object that called it. At the moment I'm trying to avoid doing this:

JavaScript code:
function Ajax() {
	var self;
	
	this.setSelf = function(obj) {
		self = obj;
	}
	
	var callback = function(response) {
		okCallback(response,self);
		
	}
}

var instance = new Ajax();
instance.setSelf(instance);
It looks horribly messy. I have literally no idea why they adopted the syntax "this.param" within a class is "this" is evaluated to be the parent, rather than "self"

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
What? That's not it at all. Basically, if you call o.f(), o is passed as the this object. If you do var f = o.f; f();, the global environment object is passed (window in a browser context). Closures are not automatically created.

Function.prototype.bind is a way around this.

Fruit Smoothies
Mar 28, 2004

The bat with a ZING

Suspicious Dish posted:

What? That's not it at all. Basically, if you call o.f(), o is passed as the this object. If you do var f = o.f; f();, the global environment object is passed (window in a browser context). Closures are not automatically created.

Function.prototype.bind is a way around this.

But ultimately the response function is always going to call the callback is going to be the $.ajax object. This is why I was trying to store it before hand within the object, so it could get passed.

From what I know about prototypes; they're meant to be the default value / function if it's not specified within a specific instance of the class.

dizzywhip
Dec 23, 2005

Personally I would just create an ajax proxy function rather than trying to create and pass around Ajax objects. But here's how you should be defining a "class".

JavaScript code:
var Ajax = function() {};

Ajax.prototype.someFunction = function() {
  // "this" now refers to the Ajax instance.
};
Alternatively you can use CoffeeScript for much nicer syntax.

CoffeeScript code:
class Ajax
  someFunction: ->

Fruit Smoothies
Mar 28, 2004

The bat with a ZING
You'll LOVE this... I wrote "var thing = Ajax()" rather than "var thing = new Ajax()" which made "this" evaluate as the window, rather than "self" within the object. Thanks for your help though, guys.

Doc Hawkins
Jun 15, 2010

Dashing? But I'm not even moving!


Lumpy posted:

You should put your own semicolons in, but not for any real concern about performance. Putting semicolons in explicitly makes it clear "what you meant" to others (and to yourself in 3 months when you revisit your code) and also avoids some functional "gotchas" where letting the interpreter put them in for you might not give you what you expect / want.

The only functional gotcha I'm aware of is lines that start with a parenthesis, which is why I prepend those with a semicolon in my otherwise semicolon-free style. :chord:

(I mention this in the hopes that this thread will tell me if it is an act of doomed hubris)

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Doc Hawkins posted:

The only functional gotcha I'm aware of is lines that start with a parenthesis, which is why I prepend those with a semicolon in my otherwise semicolon-free style. :chord:

(I mention this in the hopes that this thread will tell me if it is an act of doomed hubris)

There are other gotchas as well, so if you think that's the only one, study up. All the gotchas are rare, and easily avoided in other ways, but better safe than sorry, so I put in semi-colons.

prom candy
Dec 16, 2005

Only I may dance
Coffeescript is really nice if you're used to classical inheritance patterns and want to use them in your Javascript, I've been loving it lately. I suppose on the one hand I should be learning more about Javascript's OO problems but on the other hand even Crockford seems to like it.

Safe and Secure!
Jun 14, 2008

OFFICIAL SA THREAD RUINER
SPRING 2013

Gordon Cole posted:

Personally I would just create an ajax proxy function rather than trying to create and pass around Ajax objects. But here's how you should be defining a "class".

JavaScript code:
var Ajax = function() {};

Ajax.prototype.someFunction = function() {
  // "this" now refers to the Ajax instance.
};
Alternatively you can use CoffeeScript for much nicer syntax.

CoffeeScript code:
class Ajax
  someFunction: ->

I don't understand why we want to declare Ajax as a function rather than an object literal.

dizzywhip
Dec 23, 2005

Safe and Secure! posted:

I don't understand why we want to declare Ajax as a function rather than an object literal.

Because Fruit Smoothies wants to be able to create multiple instances of it. If you declare it as a function you can use the new operator to create an instance of it.

Safe and Secure!
Jun 14, 2008

OFFICIAL SA THREAD RUINER
SPRING 2013
Aren't functions that use the new operator to construct objects bad things that you're supposed to avoid whenever possible?

dizzywhip
Dec 23, 2005

Not really. Some people consider it to be bad because if you forget the new operator and call the function directly it'll fail silently. Personally I think that's a poor reason to avoid the feature.

If it bothers you, you can use Object.create instead, which is probably better anyways. Or just avoid prototypal inheritance altogether, but be aware of the implications of doing so.

Mindisgone
May 18, 2011

Yeah, well you know...
That's just like, your opinion man.
Still getting burnt out on this problem. I thought about doing a document.write.latlng to check the output and take it from there but I am pretty sure that variable has the value I need. I don't know javascript is beyond me :bang:.

Mindisgone posted:

Is anyone familiar with google maps api using javascript? I am specifically trying to use the geocoder api and my code is as follows:
code:

var geocoder;

function codeAddress() {
    var address = document.getElementById("zip").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
		  var latlong = results[0].geometry.location;
		  params = "latlng=latlong"
		  request = new ajaxrequest()
		  request.open("POST","cwerksignup.php",true);
          request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		  request.send(params);
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }
All I am really trying to achieve here is take the value of 'latlong' (which should be the lat and long of my query) and pass it into a php variable (or right into the html form being posted to the php script). I am not sure of the format of this variable so any info on that would be much appreciated. If I am all hosed up here please let me know.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
What do you want to do with latlng at the PHP side?

prom candy
Dec 16, 2005

Only I may dance
console.log() is your friend in these situations.

Mindisgone
May 18, 2011

Yeah, well you know...
That's just like, your opinion man.

Suspicious Dish posted:

What do you want to do with latlng at the PHP side?

I want to insert that data into a mysql table.

Mindisgone
May 18, 2011

Yeah, well you know...
That's just like, your opinion man.

prom candy posted:

console.log() is your friend in these situations.

I can just call that in the middle of the code? What sort of info will I get?

geeves
Sep 16, 2004

Mindisgone posted:

I can just call that in the middle of the code? What sort of info will I get?

You can drop anything (strings, objects, arrays, html, etc) into console.log(Obj) and it will display in Firebug or your Javascript Console.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Mindisgone posted:

I want to insert that data into a mysql table.

You need to use AJAX.

Mindisgone
May 18, 2011

Yeah, well you know...
That's just like, your opinion man.

Suspicious Dish posted:

You need to use AJAX.

....I thought that's what I was doing but apparently I am retarded.

At this junction I am only a php coder and have no familiarity to javascript but it is on my list of stuff to learn. Sorry if I'm just a complete noob.

Mindisgone fucked around with this message at 21:28 on Jul 9, 2012

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Aha, you are. Except you're sending the string "latlng=latlong" back to the server. You need to do something like "latlng=" + encodeURIComponent(JSON.stringify(latlong))

Mindisgone
May 18, 2011

Yeah, well you know...
That's just like, your opinion man.

Suspicious Dish posted:

Aha, you are. Except you're sending the string "latlng=latlong" back to the server. You need to do something like "latlng=" + encodeURIComponent(JSON.stringify(latlong))

That's quite a mouthful but I will definitely try this out. I guess without telling it the encoding it doesn't send the variable just a string literal?

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Mindisgone posted:

That's quite a mouthful but I will definitely try this out. I guess without telling it the encoding it doesn't send the variable just a string literal?

What you're doing is the php equivalent of:

PHP code:
$latlong = results[0]['geometry']['location'];
$params = 'latlng=$latlong'; // note the single quotes
What you could do in php (still using single quotes):
PHP code:
$latlong = results[0]['geometry']['location'];
$params = 'latlng=' . $latlong;
Which would be the same as

JavaScript code:
var latlong = results[0].geometry.location;
params = "latlng=" + latlong;

Mindisgone
May 18, 2011

Yeah, well you know...
That's just like, your opinion man.

Wheany posted:

What you're doing is the php equivalent of:

PHP code:
$latlong = results[0]['geometry']['location'];
$params = 'latlng=$latlong'; // note the single quotes
What you could do in php (still using single quotes):
PHP code:
$latlong = results[0]['geometry']['location'];
$params = 'latlng=' . $latlong;
Which would be the same as

JavaScript code:
var latlong = results[0].geometry.location;
params = "latlng=" + latlong;

Well you do a good job of pointing out unnecessary redundancy. What I really want is:
PHP code:
$latlong = results[0]['geometry']['location'];
$params = $latlong;
In actuality I don't even need that, I just need to send the value of "$latlong" back through the ajax which I assume is:
JavaScript code:
request.send(latlong);
The short version of course.

prom candy
Dec 16, 2005

Only I may dance
That's not how HTTP requests work though. You can't just go "hey, server, 41.32,71.23!"

You need to send it as a named parameter, someone already posted the right code for you above.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Of course you can. It would be in the body of a POST request.

Mindisgone
May 18, 2011

Yeah, well you know...
That's just like, your opinion man.

prom candy posted:

That's not how HTTP requests work though. You can't just go "hey, server, 41.32,71.23!"

You need to send it as a named parameter, someone already posted the right code for you above.

so I still need:
code:
var latlong = results[0].geometry.location;
params = "latlng=" + latlong;

Polio Vax Scene
Apr 5, 2009



Isn't results[0].geometry.location the same as results[0]['geometry']['location']?

Mindisgone
May 18, 2011

Yeah, well you know...
That's just like, your opinion man.

Manslaughter posted:

Isn't results[0].geometry.location the same as results[0]['geometry']['location']?

that was being used as an example of the php equivalent.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Mindisgone posted:

so I still need:
code:
var latlong = results[0].geometry.location;
params = "latlng=" + latlong;

Not quite. You still need to encode it to JSON.

prom candy
Dec 16, 2005

Only I may dance

Suspicious Dish posted:

Of course you can. It would be in the body of a POST request.

Fair enough, you can.

You shouldn't.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

prom candy posted:

Fair enough, you can.

You shouldn't.

Why not?

putin is a cunt
Apr 5, 2007

BOY DO I SURE ENJOY TRASH. THERE'S NOTHING MORE I LOVE THAN TO SIT DOWN IN FRONT OF THE BIG SCREEN AND EAT A BIIIIG STEAMY BOWL OF SHIT. WARNER BROS CAN COME OVER TO MY HOUSE AND ASSFUCK MY MOM WHILE I WATCH AND I WOULD CERTIFY IT FRESH, NO QUESTION

Suspicious Dish posted:

Of course you can. It would be in the body of a POST request.

I think he meant "can't" in the same way you "can't" go around killing people. Theoretically you could, but it's a better idea not to.


Because if you don't name the parameter it's a meaningless bit of information that only you know the meaning of. You're basically asking the equivalent of "why bother naming variables?" The answer is because it serves as a label so you can easily identify what the data you're seeing is supposed to represent without having to trace it back to the source every time.

putin is a cunt fucked around with this message at 05:33 on Jul 10, 2012

prom candy
Dec 16, 2005

Only I may dance
Why would you? Wouldn't it be best to send it as a param, or convert it to JSON or some other specification to give it some context and meaning so that the guy who inevitably takes over the codebase has some idea of what's going on?

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
The API returns a LatLng, so you would still have to call toUrlValue() on it anyway.

I guess I don't see why adding an extra layer of application/x-www-form-urlencoded is a good idea. If you POST to /save-geolocation.php with geolocation data like 32.5,28.9, I think it's fairly clear.

putin is a cunt
Apr 5, 2007

BOY DO I SURE ENJOY TRASH. THERE'S NOTHING MORE I LOVE THAN TO SIT DOWN IN FRONT OF THE BIG SCREEN AND EAT A BIIIIG STEAMY BOWL OF SHIT. WARNER BROS CAN COME OVER TO MY HOUSE AND ASSFUCK MY MOM WHILE I WATCH AND I WOULD CERTIFY IT FRESH, NO QUESTION

Suspicious Dish posted:

The API returns a LatLng, so you would still have to call toUrlValue() on it anyway.

I guess I don't see why adding an extra layer of application/x-www-form-urlencoded is a good idea. If you POST to /save-geolocation.php with geolocation data like 32.5,28.9, I think it's fairly clear.

Because it's clearer. Labelling data just makes sense, I'm surprised this is at all controversial.

Adbot
ADBOT LOVES YOU

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde

Suspicious Dish posted:

I guess I don't see why adding an extra layer of application/x-www-form-urlencoded is a good idea. If you POST to /save-geolocation.php with geolocation data like 32.5,28.9, I think it's fairly clear.
That's the problem. It isn't.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply