EmberJS 路由器模型公布后

2018-01-03 18:22 更新

描述

使用afterModel或重定向鉤子來(lái)獲取有關(guān)重定向的當(dāng)前模型的信息。它們接收兩個(gè)參數(shù),即分解模型和轉(zhuǎn)換。

語(yǔ)句

Ember.Route.extend({
  afterModel: function(posts, transition) {
      this.transitionTo('routeName', posts.get('firstObject'));
   }
});

例子

<!DOCTYPE html>
<html>
   <head>
      <title>Emberjs After the Model is Known</title>
      <!-- CDN's -->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script>
      <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.min.js"></script>
      <script src="https://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script>
      <script src="https://builds.emberjs.com/release/ember.debug.js"></script>
      <script src="https://builds.emberjs.com/beta/ember-data.js"></script>
   </head>
   <body>
      <script type="text/x-handlebars" data-template-name="application">
         {{outlet}}
      </script>

      <script type="text/x-handlebars" data-template-name="index">
         <a href="#" {{action "getIds"}}>Click To Get IDs Of Student</a>
      </script>

      <script type="text/x-handlebars" data-template-name="posts">
         <b>Student Id:</b> <i>{{stud_id}} </i><br/>
      </script>

      <script type="text/javascript">
         App = Ember.Application.create({});

         App.Router.map(function() {
            this.route('posts', { path: 'school/:stud_id'});
         });

         App.IndexController = Ember.Controller.extend({
            actions: {
               getIds: function() {
                  this.transitionToRoute('posts', { stud_id: 1 });
               }
            }
         });

         App.IndexRoute = Ember.Route.extend({
             afterModel: function(params) {
                return params;
             }
         });
      </script>
   </body>
</html>

輸出

讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:

  • 將上面的代碼保存在routing_aftr_mod.html文件中

  • 在瀏覽器中打開(kāi)此HTML文件。

以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)