如何让Ember组件对象在promise成功或错误回调?

Nit*_*791 2 javascript ember.js

我已经传递了两个回调函数成功和使用then方法从ajax调用返回的promise的错误.现在我无法在成功/错误方法中获得Ember组件对象.

import Ember from 'ember';

export default Ember.Component.extend({
    data:null,
    issueType:'',
    description:null,
    prepareSubmitRaiseIssueModal:function(){
        var data = this.get('data');
        this.set('ticket.category',data.category);
        this.set('ticket.name',this.get('session.currentUser.first_name'));
        this.set('ticket.phone',this.get('session.currentUser.phone'));
        this.set('ticket.groupId',data.groupId);
        this.set('ticket.ownerId',this.get('session.currentUser.id'));
        this.set('ticket.oyoId',this.get('session.currentOwnerHotelOyoId'));
        this.set('ticket.ticketRaisedBy','owner');
        this.set('ticket.bookingId',data.bookingId);
        this.set('ticket.subType',data.subType);
        this.set('ticket.subSubIssue',data.subSubIssue);
        this.set('ticket.email',this.get('ticket.oyoId')+'@oyoproperties.com');
        this.set('ticket.subject',this.get('ticket.oyoId')+' : '+this.get('ticket.category'));
        this.set('ticket.description',this.get('description'));
    },
    success:function(){
        console.log(this.get('description'));
    },
    error:function(){
        console.log(this.get('description'));
    },
    actions :{
        submitIssue:function(){
            this.prepareSubmitRaiseIssueModal();
            this.get('ticket').submitRaiseIssue().then(this.success,this.error);
            //this.send('closeRaiseIssueModal');
        },
        closeRaiseIssueModal:function(){
            this.sendAction('closeRaiseIssueModal');
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

我能够获得Ember组件对象,而不是传递命名函数我传递匿名函数.

submitIssue:function(){
            var self = this;
            this.prepareSubmitRaiseIssueModal();
            this.get('ticket').submitRaiseIssue().then(function(response){
                console.log(self.get('description'));
            },
            function(err){
                console.log(self.get('description'));
            });
            //this.send('closeRaiseIssueModal');
        },
Run Code Online (Sandbox Code Playgroud)

有什么办法可以让Ember组件对象的前一个案例的参考?

kri*_*ris 5

哇说意大利面.

prepareSubmitRaiseIssueModal:function(){
        var data = this.get('data');
        this.set('ticket.category',data.category);
        this.set('ticket.name',this.get('session.currentUser.first_name'));
        this.set('ticket.phone',this.get('session.currentUser.phone'));
        this.set('ticket.groupId',data.groupId);
        this.set('ticket.ownerId',this.get('session.currentUser.id'));
        this.set('ticket.oyoId',this.get('session.currentOwnerHotelOyoId'));
        this.set('ticket.ticketRaisedBy','owner');
        this.set('ticket.bookingId',data.bookingId);
        this.set('ticket.subType',data.subType);
        this.set('ticket.subSubIssue',data.subSubIssue);
        this.set('ticket.email',this.get('ticket.oyoId')+'@oyoproperties.com');
        this.set('ticket.subject',this.get('ticket.oyoId')+' : '+this.get('ticket.category'));
        this.set('ticket.description',this.get('description'));
    },
Run Code Online (Sandbox Code Playgroud)

怎么样

prepareSubmitRaiseIssueModal:function(){
        var data = this.get('data');
        var ticket = this.get('ticket')
        ticket.setProperties({
           'category': data.category,
           'name': ...
        })

    },
Run Code Online (Sandbox Code Playgroud)

要传递参考文献,您可以使用

promise.then(function() {
     this.mysuccess();
}.bind(this), function() {
     this.myerror();
}.bind(this))
Run Code Online (Sandbox Code Playgroud)
const self = this; 
promise.then(function() { 
    self.blah();
});
Run Code Online (Sandbox Code Playgroud)
promise.then(result => { 
    this.blah();
}) 
Run Code Online (Sandbox Code Playgroud)

在你的情况下,我会写一个实用程序JS文件来显示通知.

并亲自处理每个承诺的成功,并以一般错误方法处理错误.

utils的/ notifications.js

function reportError(error) {
    displayNotification('error', getErrorMessage(error));
}
Run Code Online (Sandbox Code Playgroud)
import reportError from 'utils/notifications';

export default Ember.Controller.extend({
....
promise.then(result => {
   // Do custom stuff with result;
}, reportError);
....
});
Run Code Online (Sandbox Code Playgroud)

并承诺承诺

return promise1.then(x => {
   return promise2.then(x2 => {
       return promise3 ... etc
   })
}).catch(reportError); // Single hook needed but u need to return promises
Run Code Online (Sandbox Code Playgroud)