简体   繁体   中英

Ember RSVP promise stopped working

For some reason an RSVP promise I created in an action stopped working for me and I can't figure out what when wrong where to make it stop working.

I have a component that sends an action and waits for a response

// COMPONENT IN ITEM ROUTE
callAjaxAction() {
  this.setProperties({working:true});
  Ember.RSVP.cast(this.attrs.action()).finally(() => {
    Ember.$('.draggable').animate({
      left: 0
    });
    this.setProperties({working:false});
  });
}

This particular instance of the component calls this action in the controller

// IN ITEM CONTROLLER
placeBid() {
  return new Ember.RSVP.Promise((resolve,reject) => {
    if(this.get('winning')) {
      this.get('notification')._confirm({message: "You are currently the highest bidder, would you like to continue with your bid?",isConfirm: true}).then(()=>{
        console.log('confirmed');
        this.send('placeBidActual').then(()=>{
          resolve();
        }).catch(()=>{
          reject();
        });
        return true;
      }).catch(()=>{
        resolve();
        console.log('denied');
        return false;
      });
    } else {
      setTimeout(()=>{
        this.send('placeBidActual').then(()=>{
          resolve();
        }).catch(()=>{
          reject();
        });
      },500);
    }
  });
}

This action is calling a confirm method on a service and waiting for the user to hit yes in the case of the user already winning this item. Otherwise I'm just calling the actual ajax action right away, that action looks like this

// IN ITEM CONTROLLER
placeBidActual() {
  return new Ember.RSVP.Promise((resolve,reject) => {
    Ember.$.ajax({
      ...
    }).then((response)=>{
      (do some stuff with the response)
      resolve();
    }, (reason)=>{
      (do something with rejection reason)
      reject(reason);
    });
  });
}

In the console I'm getting the error

Uncaught TypeError: Cannot read property 'then' of undefined

On the line where it states this.send('placeBidActual')

UPDATE:

Here is maybe a better explanation to the expected process flow.

The user attempts to place a bid, the user swipes a component over to indicate they wish to bid. The UI at this point shows a loading indicator and waits for the ajax action to complete before it resets the UI. If the user is not already the highest bidder of the item it will go straight to the ajax action and upon completion signifies to the component to reset the UI. However, if the user is the highest bidder of the item it should instead show a confirm message (using the notification service I have setup) and wait for the user to either confirm or deny. If they deny it just cancels and signifies to the component to reset the UI. If the user confirms then it calls the ajax action and upon completion signifies to the component to reset the UI.

Updated answer: This isn't working because send doesn't return the value of the action.

I suggest moving the placeBidActual action to a method on the controller and call it like any normal method. This way you will get the return value and be able to call .then on it.

You should pass a function, whithout invoke it.

Instead this: Ember.RSVP.cast(this.attrs.action()).finally(() =>

Try it: Ember.RSVP.cast(this.attrs.action).finally(() =>

Invoking the funcion this.attrs.action() does it pass undefined for the Promise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM