Emberjs-routing-prvnt-trans

提供:Dev Guides
移動先:案内検索

willTransitionによる遷移を防ぐルーター

_ \ {\ {link-to}} _ヘルパーまたは_transitionTo_メソッドを使用して移行を再試行すると、現在アクティブなルートで_willTransition_アクションが起動されます。

構文

Ember.Route.extend ({
   actions: {
      willTransition(transition) {
        //handle the transition
      }
   }
});

以下の例は、アクティブルートでの willTransition アクションによる遷移の防止を示しています。 willtransitionというルートを作成し、次のコードで_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
});

Router.map(function() {
   this.route('willtransition');
});

//It specifies Router variable available to other parts of the app
export default Router;

_application.hbs_ファイルを作成し、次のコードを追加します-

//link-to is a handlebar helper used for creating links
{{link-to 'Click For Transition' 'willtransition'}}
{{outlet}}//It is a general helper, where content from other pages
   will appear inside this section

次のコードで_app/routes/_の下に作成されたファイル_willtransition.js_ファイルを開きます-

import Ember from 'ember';

export default Ember.Route.extend ({
   actions: {
      willTransition(transition) {

        //decalring the self variable
         var self = this;

        //checking whether self variable is false or not
         if (!this.get('allowTransition')) {
            document.write('<b><font color = "red">');

           //display the message
            document.write("transition abort");
            document.write('</font><br>');
            transition.abort(); //calling abort function

            Ember.run.later(function () {
              //setting the self variable to true
               self.set('allowTransition', true);
               document.write('<b><font color = "blue">');

              //display the message
               document.write("transition retry");
               document.write('</font>');
               transition.retry(); //calling retry function
            }, 500);
         }
      }
   }
});

次のコードで_app/templates/_の下に作成された_willtransition.hbs_ファイルを開きます-

<h2>Hello...Welcome to finddevguides!!!</h2>
{{outlet}}

出力

エンバーサーバーを実行すると、次の出力が表示されます-

Ember.jsは移行します

リンクをクリックすると、データが表示されます。 ただし、戻るリンクをクリックすると、_willTransition_アクションが_transition.abort()_メソッドを呼び出してから_transition.retry()_メソッドを呼び出します。

Ember.jsは移行します