【问题标题】:Ember JS - transitionTo route and then display Feedback messageEmber JS - transitionTo 路由然后显示反馈消息
【发布时间】:2021-02-11 17:16:35
【问题描述】:

由于我是一个新手并且刚开始做一些前端,所以我遇到了 EmberJS transitionTo() 的问题。 问题是当我在产品的详细信息中单击“删除”时,它会在那里显示反馈消息,然后它会转移到主页。 如果我在详细信息,我想先跳转到主页,然后显示反馈消息。

我的代码:

delete: function(system,) {
  const self = this;
  this.get('data').del(systemId)
    .then(function() {
      self.transitionTo('home');
      self.setFeedbackSuccess(self.currentModel, self.get('intl').t('delete.success'));
    }).then(() => self.refresh())
    .then(() => {
      self.transitionTo('home');
      self.setFeedbackSuccess(self.currentModel, self.get('intl').t('delete.success'));
    })
    .catch((errorObject) => {..})
    .catch((errorObject) => self.errorHandler(errorObject, self.currentModel));
},

对函数的第一次调用是在详细信息页面上显示反馈的位置,如果我从那里删除它,在 .then(() => self.refresh()) 之后它将反馈中不显示任何内容,除非我在主页上,然后从那里删除。

【问题讨论】:

    标签: javascript ember.js


    【解决方案1】:

    所以有几种方法可以做到这一点。开始 transitionTo 返回一个 thenable 对象,以便您可以执行以下操作:

      this.get('data').del(systemId)
        .then(() => this.transitionTo('home'))
        .then(() => this.setFeedbackSuccess(this.currentModel, this.get('intl').t('delete.success'));
    

    这假设setFeedbackSuccess 挂钩到一些全局通知系统,这些通知系统可以从新路由中看到。或者,您可以装饰过渡对象

      this.get('data').del(systemId)
        .then(() => {
           let transition = this.transitionTo('home'));
           transition.data.feedback = { 
             model: this.currentModel, 
             message: this.get('intl').t('delete.success')
           };
           return transition
        });
    

    然后在您的主路由中,在activateafterModel 等钩子之一中,您可以调用setFeedbackSuccess

    afterModel(model, transition) {
      this._super(model, transition); // or super.afterModel(model, transition) if using native classes
      if (transition.data.feedback) {
        this.setFeedbackSuccess(transition.data.feedback.model, transition.data.feedback.message);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      • 2013-01-22
      • 1970-01-01
      • 1970-01-01
      • 2021-02-08
      相关资源
      最近更新 更多