admin管理员组文章数量:1289483
I'm trying to set up an object so that it has an encapsulated $.getJSON method. Here's my setup:
function Property(price, deposit){
this.price = price;
this.deposit = deposit;
this.getMortgageData = function(){
$.getJSON('='+this.price+'&d='+this.deposit+'&c=?', function(data){
this.mortgageData = data;
});
}
return true;
}
Now the problem seems to be that I don't have access to 'this' inside the getJSON callback function which makes sense.
Is there a workaround for this type of function or am I just thinking about this totally the wrong way? I've only ever really coded using PHP OO before so JS OO is a bit new to me.
Other things I've tried are:
function Property(price, deposit){
this.price = price;
this.deposit = deposit;
this.getMortgageData = function(){
this.mortgageData = $.getJSON('='+this.price+'&d='+this.deposit+'&c=?', function(data){
return data;
});
}
return true;
}
But still,
var prop = new Property();
prop.getMortgageData();
// wait for the response, then
alert(prop.mortgageData.xyz); // == undefined
I'm trying to set up an object so that it has an encapsulated $.getJSON method. Here's my setup:
function Property(price, deposit){
this.price = price;
this.deposit = deposit;
this.getMortgageData = function(){
$.getJSON('http://example./index?p='+this.price+'&d='+this.deposit+'&c=?', function(data){
this.mortgageData = data;
});
}
return true;
}
Now the problem seems to be that I don't have access to 'this' inside the getJSON callback function which makes sense.
Is there a workaround for this type of function or am I just thinking about this totally the wrong way? I've only ever really coded using PHP OO before so JS OO is a bit new to me.
Other things I've tried are:
function Property(price, deposit){
this.price = price;
this.deposit = deposit;
this.getMortgageData = function(){
this.mortgageData = $.getJSON('http://example./index?p='+this.price+'&d='+this.deposit+'&c=?', function(data){
return data;
});
}
return true;
}
But still,
var prop = new Property();
prop.getMortgageData();
// wait for the response, then
alert(prop.mortgageData.xyz); // == undefined
Share
Improve this question
asked Jan 27, 2011 at 15:56
David TuiteDavid Tuite
22.7k25 gold badges114 silver badges179 bronze badges
1 Answer
Reset to default 8Your first attempt is close, but as you said, you can't access this
inside the callback because it refers to something else. Instead, assign this
to another name in the outer scope, and access that. The callback is a closure and will have access to that variable in the outer scope:
function Property(price, deposit){
this.price = price;
this.deposit = deposit;
var property = this; // this variable will be accessible in the callback, but still refers to the right object.
this.getMortgageData = function(){
$.getJSON('http://example./index?p='+this.price+'&d='+this.deposit+'&c=?', function(data){
property.mortgageData = data;
});
}
return true;
}
本文标签: ajaxUsing getJSON() with callback within a Javascript objectStack Overflow
版权声明:本文标题:ajax - Using $.getJSON() with callback within a Javascript object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741421070a2377807.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论