MrFish Posted November 21, 2011 Share Posted November 21, 2011 Ok I've got another question that should be easier to answer. I've got an object called Frame that holds an array of objects called Points. I want to use jQuery's $.get method to call a php file to return this information. The $.get is called inside the Frame object. function Frame(){this.JSONReturnData = null;this.pointsJSON = null; this.loadPoints = function(){ $.get("php/getPoints.php", {sessionId:window.sessionId,sequence:this.sequence}, function(d) { this.JSONReturnData = d; this.pointsJSON = $.parseJSON(d); }}} Here is the code cleaned up a bit. I set the values to null in the head of the objects and I want to set their actual values in the method "loadPoints". But I realised before I even tested it that the $.get callback is going to run inside it's own object. And if I return the data then it will just return to the $ (jQuery) object. So using this and return won't work in this situation and I'm not sure how to send the data back to the host object. I know I could use window to set it as a global variable but that's just bad programming. I'm trying to get away from cheap dirty tricks like that. Anyone know how I can solve this problem? EDIT: I have thought about doing this- var jObj = $.get("php/getPoints.php", {sessionId:window.sessionId,sequence:this.sequence}, function(d) { return d; }); this.JSONReturnData = jObj.responseText; this.pointsJSON = $.parseJSON(this.JSONReturnData); But the issue is that the rest of the method will keep running and not wait for the $.get method to finish. Link to comment Share on other sites More sharing options...
Ingolme Posted November 21, 2011 Share Posted November 21, 2011 There is a scope issue. I think this might solve the problem: function Frame(){var that = this;this.JSONReturnData = null;this.pointsJSON = null; this.loadPoints = function(){ $.get("php/getPoints.php", {sessionId:window.sessionId,sequence:this.sequence}, function(d) { that.JSONReturnData = d; that.pointsJSON = $.parseJSON(d); }}} Link to comment Share on other sites More sharing options...
MrFish Posted November 21, 2011 Author Share Posted November 21, 2011 Oh nice. I didn't know that was possible. Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.