0

My previous question was a duplicate, I read the answer from the post it duplicated but no luck.

I've added in a bind as suggested in the answers in the SO post. But I am still getting undefined.

I have a series of promises, before each execution, I check whether the user has cancelled the promise chain.

My issue is, I cannot access 'this' inside the call back methods, a crude example is below, I cannot access the p.test variable inside getMoreData()

p.test = 'hello!';

p.init = function(){ 
    var self = this;

    this.getData() 
        .then(function(data) {
            return self.shouldContinue(getMoreData,data).bind(self);
        });
}

p.shouldContinue = function(cb, data) {
    // ...
    this.currentRequest = cb.call(this,data);
};

p.getData = function(){
    // return ajax call
};

p.getMoreData = function(){
    console.log(this.test); // undefined
    // return ajax call
};
Community
  • 1
  • 1
panthro
  • 22,779
  • 66
  • 183
  • 324
  • I've amended the question with new code. The suggestion did not work. – panthro Jun 23 '15 at 13:24
  • 3
    Because you didn't define `self` at `return self.shouldContinue(getMoreData,data).bind(self);` – fuyushimoya Jun 23 '15 at 13:25
  • This is exactly my code, what do you mean? – panthro Jun 23 '15 at 13:26
  • I mean, you did't do something like `var self = this;` before any thing, so `self` is `undefined`. – fuyushimoya Jun 23 '15 at 13:27
  • Self is defined. So this is not the issue. – panthro Jun 23 '15 at 13:28
  • Hang on; I think there are some important parts to this suggested by your code comments that may not have been noticed. An AJAX call can register a callback in two ways; one, by passing one into the ajax function, or two, by returning something to the effect of a "Promise object" that you can return through the call chain, and call `prom.then(function() {})` on. But I'm not certain you're doing either. You have no arguments to `getMoreData`, and you're never calling `.then` - you're treating the result of `getMoreData` like it's a function. Adding a bit more code would help explain the intent. – Katana314 Jun 23 '15 at 14:00

3 Answers3

0
return self.shouldContinue(getMoreData,data).bind(self);

It looks like you're accidentally calling shouldContinue, and trying to bind its result, rather than binding the function without calling it. This should get you what you need.

return self.shouldContinue.bind(self, self.getMoreData, data);

Remember that getMoreData is a function that's not defined under variable scope, so it needs to be accessed with a .

EDIT: Also noticed shouldContinue has a similar issue, but I'm confused by the intent of some parts of your code so I've added a comment to ask for more information.

Katana314
  • 8,429
  • 2
  • 28
  • 36
  • Thanks, but self.shouldContinue.bind(self, self.getMoreData, data); now no longer calls the getMoreData fnc – panthro Jun 23 '15 at 13:39
0

Did you try this?
You need to bind this into the cb

p.test = 'hello!';

p.init = function(){
var self = this;
this.getData() 
    .then(function(data) {
        return self.shouldContinue(getMoreData,data).bind(self);
    })
}

p.shouldContinue = function(cb, data){
   ...
  this.currentRequest = cb(this,data).bind(this);
};

p.getData = function(){
    //return ajax call
};

p.getMoreData = function(){
    console.log(this.test); //undefined
    //return ajax call
};
jsam
  • 301
  • 1
  • 7
0

Change to this:

 p.init = function(){ 
      var self = this;

      this.getData() 
          .then(function(data) {
              return self.shouldContinue.bind(self, self.getMoreData, data)();
          });
  }
  1. bind self.shouldContinue to self and give it appropriate param.

  2. .bind returns a Function, so, you need to call it.So the self.shouldContinue will continues to call self.getMoreData, otherwise it left there waiting to be called.

fuyushimoya
  • 9,715
  • 3
  • 27
  • 34