Emberjs-router-load-substates
提供:Dev Guides
EmberJS-サブステートの読み込み/エラー
Ember.jsは、エラーを利用してサブステートをロードすることにより、ルート間の非同期をカスタマイズするための遷移をオーバーライドします。
構文
Ember.Route.extend ({
model() {
//code here
}
});
Router.map(function() {
this.route('path1', function() {
this.route('path2');
});
});
例
以下の例は、ルートのロード中に発生するロード/エラーサブステートの使用方法を示しています。 新しいルートを作成し、loaderrorという名前を付け、次のコードで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('loaderror', function() {
this.route('loaderr');
});
});
//It specifies Router variable available to other parts of the app
export default Router;
次のコードで_app/routes/_の下に作成されたファイル_loaderror.js_ファイルを開きます-
import Ember from 'ember';
export default Ember.Route.extend ({
model() {
return new Ember.RSVP.Promise(function (resolve, reject) {
setTimeout(function () {
resolve({});
}, 1500);
});
}
});
次のコードでapp/templates/の下に作成されたファイルapplication.hbsを開きます-
{{outlet}}
ファイル_index.hbs_を開き、次のコードを追加します-
{{link-to 'loaderror' 'loaderror'}}
<small>(this link displays the 'loading' route/template correctly)</small>
{{outlet}}
loaderrorリンクをクリックすると、ページが読み込み状態で開きます。 したがって、ロード状態を指定するためにloading.hbsファイルを作成します-
<h2 style = "color: #f00;">template: loading</h2>
エラーメッセージを表示する_loaderror.hbs_ファイルを開きます-
<h2>--error--!</h2>
{{link-to 'loaderror.loaderr' 'loaderror.loaderr'}}
<small>(doesn't display the 'loading' route/template,
because 'loaderror/loading' does not exist!!!</small>
{{outlet}}
出力
エンバーサーバーを実行すると、次の出力が表示されます-
リンクをクリックすると、テンプレートの読み込みメッセージが表示されます-
次に、遷移中にエラーが発生したときにエラーのサブ状態を表示します-