我试图从成功回调函数中设置的参数似乎有问题:
var CampModel = CampDataModel.extend({
initialize : function(){
this.fetchActiveAndPending();
console.log(this.get('active'));
},
//Counts active and pending campaigns for front page.
CountActiveAndPending : function(data){
var active = 0;
var pending = 0;
$.each(data.returnValue,function(index,val){
if (val.ApprovedOnSite){
active++;
}
else
pending++;
});
this.set('active',active);
this.set('pending',pending);
},
//fetches data from server using campModel.
fetchActiveAndPending : function(){
console.log('fetching!');
that = this;
this.fetch({
success:function(model,response){
that.CountActiveAndPending(response);
}
});
}
});
return CampModel;
});
this.get('active') 的结果始终是默认数字。如果我尝试在成功回调函数中使用 this.get('active') ,它会给出正确的结果。是否可以从回调函数中设置一个 var 并从外部调用它,比如说初始化函数?