Emberjs-router-specify-routes-model
提供:Dev Guides
EmberJS-ルートのモデルの指定
データテンプレートと同じ名前のルートでテンプレート名を定義し、そのモデルフックを実装することにより、ルートモデルを指定できます。
Ember.Route.extend ({
model: function() {
return {//value-1 },{//value-2 },{..},{//value-n };
}
});
上記のコードでは、value-1からvalue-nの変数は、テンプレートで呼び出される値を格納するために使用されます。
次の表は、ルートの指定モデルのさまざまなタイプを示しています-
S.No. | Specifying Routes & Description |
---|---|
1 |
EmberがURLから値にアクセスするために使用する動的セグメントを持つルートを定義します。 |
2 |
_RSVP.hash_を使用して複数のモデルを定義できます。_RSVP.hash_はさらにオブジェクトを使用してプロミスを返します。 |
例
次の例は、データを表示するためのルートを指定する方法を示しています。 前の章で指定された新しいルートを作成します。 次のコードで_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('specifyroute');
});
//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 'specifyroute'}}Click here to see details{{/link-to}}
{{outlet}}//It is a general helper, where content from other pages will
appear inside this section
次のコードで_app/templates/_の下に作成された_specifyroute.hbs_ファイルを開きます-
<h2>List of Players</h2>
<ul>
//The <i>each</i> helper to loop over each item in the array provided from model() hook
{{#each model as |player|}}
<li>{{player}}</li>
{{/each}}
</ul>
{{outlet}}
URLを構築するには、値を返すモデルを実装する必要があります-
import Ember from 'ember';
export default Ember.Route.extend ({
//The model() method returns the data which you want to make available to the template
model() {
return ['MS Dhoni', 'Steve Smith', 'Jason Roy','AB de Villiers','Kane Williamson'];
}
});
出力
エンバーサーバーを実行すると、次の出力が表示されます-
出力のリンクをクリックすると、次のスクリーンショットのように結果が生成されます-