I am trying to do something rather like the following:
function newCounter(){
return {
"counter" : 0
,"mode" : "new"
,"start" : function( arg_function ){
// Run this before counting.
this.counter = 0;
this.mode = "count";
}
,"finished" : function(){
// Run this when counting is no longer allowed.
this.mode = "done";
}
,"increment" : function(arg_key){
globalThing.isValid(arg_key)
.done(function(data){
if( this.mode === "count" ){
this.counter++;
}
});
}
}
}
Now, the problem here, as one may notice, is that inside the .done()
section there, I have a reference to this.
- which does not and cannot refer to the object in question because it's inside a promise with a generic function, and as such, refers to window.
and not the specific object being referenced from. I have tried these:
.done(function(data){
if( this.mode === "count" ){
this.counter++;
}
}.apply(this))
.done(function(data){
if( this.mode === "count" ){
this.counter++;
}
}.call(this))
as solutions, but they have not succeeded. I'm not entirely sure why. If you can see what I'm trying to do here ... could you recommend, please, a solution to my woes?