Emberjs-routing-prmis
提供:Dev Guides
EmberJS-約束のためのルーターの一時停止
遷移は、モデルフックからpromiseを返すことで一時停止できます。 遷移は、モデルから通常のオブジェクトまたは配列を返すことですぐに完了できます。
構文
Ember.Route.extend ({
model() {
return new Ember.RSVP.Promise(function(param) {
//code here
});
}
});
例
以下の例は、モデルがプロミスを返す場合に遷移が一時停止する方法を示しています。 新しいルートを作成し、_promisepause_という名前を付け、_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('promisepause');
});
//It specifies Router variable available to other parts of the app
export default Router;
次のコードで_app/templates/_の下に作成されたファイル_application.hbs_ファイルを開きます-
<h2>Router Pauses for Promises</h2>
{{#link-to 'promisepause'}}Click Here{{/link-to}}
上記のリンクをクリックすると、Promise一時停止テンプレートページが開きます。 _promisepause.hbs_ファイルには次のコードが含まれています-
{{model.msg}}
{{outlet}}
次のコードで_app/routes/_の下に作成された_promisepause.js_ファイルを開きます-
import Ember from 'ember';
import RSVP from 'rsvp';
export default Ember.Route.extend ({
model() {
//RSVP.js is an implementation of Promises
return new Ember.RSVP.Promise(function (resolve) {
Ember.run.later(function () {
//display the promise message
resolve ({
msg: "This is Promise Message..."
});
}, 3000);//time in milli second to display promise
});
}
});
出力
エンバーサーバーを実行すると、次の出力が表示されます-
あなたがリンクをクリックすると、モデルは3秒まで解決されない約束を返し、約束が満たされると、ルータは移行を開始します-