Return a promise from a controller action in Ember?

后端 未结 1 1690
被撕碎了的回忆
被撕碎了的回忆 2020-12-05 07:54

I have a component that needs to communicate with a controller and eventually perform some clean up after the controller says everything is ok (ie, jQuery \"un\"-in

相关标签:
1条回答
  • 2020-12-05 08:58

    send and sendAction are one way streets, that being said, you can send a defer to the action and expect it to resolve/reject it.

    var defer = Ember.RSVP.defer();
    
    defer.promise.then(function(resolvedValue){
      alert(resolvedValue); 
    });
    
    setTimeout(function(){
      defer.resolve('hello world');
    },2000);
    

    Yours would look a bit like this

    var defer = Ember.RSVP.defer(),
        self = this;
    
    defer.promise.then(function(){
      self.closeModal();
    },
    function(){
      alert('error');
    });
    
    this.sendAction('save', defer);
    

    save action

    actions: {
      save: function(defer){
    
        // if succeeded 
        defer.resolve();
    
        // or if failure occurs 
        defer.reject();
      }
    }
    

    Be careful, you want to make sure you don't leave out the reject route, you'd hate to have the modal stuck up there because the save failed and there wasn't a failure method.

    Sorry I didn't convert to coffee script, I figure you'll either understand or convert and understand and I won't have given you a wrong answer.

    0 讨论(0)
提交回复
热议问题