当前位置: 首页 > 文档资料 > EmberJS 入门教程 >

从拒绝中恢复(Recovering from Rejection)

优质
小牛编辑
117浏览
2023-12-01

承诺拒绝可以缓存在模型钩子中,可以将其转换为不会使转换停止的履行。

语法 (Syntax)

Ember.Route.extend ({
   model() {
      //return the recovery message
   }
});

例子 (Example)

下面给出的示例显示了如果模型拒绝承诺,将如何中止转换。 创建一个新路由并将其命名为promisereject并打开router.js文件以定义URL映射 -

import Ember from 'ember';                   
//Access to Ember.js library as variable Ember
import config from './config/environment';
//It provides access to app's configuration data as variable config 
//The const declares read only variable
const Router = Ember.Router.extend ({
   location: config.locationType,
   rootURL: config.rootURL
});
//Defines URL mappings that takes parameter as an object to create the routes
Router.map(function() {
   this.route('recoveryrejection');
});
//It specifies Router variable available to other parts of the app
export default Router;

使用以下代码打开在app/templates/下创建的application.hbs文件 -

<h2>Recovering from Rejection</h2>
{{#link-to 'recoveryrejection'}}Click Here{{/link-to}}

当您单击上面的链接时,它将打开recoveryrejection模板页面。 recoveryrejection.hbs文件包含以下代码 -

{{model.msg}}
{{outlet}}

现在使用以下代码打开在app/routes/下创建的recoveryrejection.js文件 -

import Ember from 'ember';
import RSVP from 'rsvp';
export default Ember.Route.extend ({
   model() {
      //returning recovery message
      return {
         msg: "Recovered from rejected promise"
      };
   }
});

输出 (Output)

运行ember服务器,您将收到以下输出 -

Ember.js恢复拒绝

当您点击链接时,承诺将被拒绝,它将显示恢复消息以继续过渡 -

Ember.js恢复拒绝