Emberjs-router-render-template
提供:Dev Guides
EmberJS-テンプレートのレンダリング
ルートは、外部テンプレートを画面にレンダリングするために使用されます。これは、ルートハンドラーで_templateName_を定義することで実現できます。
構文
Ember.Route.extend ({
templateName: 'path'
});
例
次の例は、データを表示するためのテンプレートをレンダリングする方法を示しています。 前の章で指定された新しいルートを作成します。 ここでは、投稿としてルートを作成し、URLマッピングを定義する次のコードで_router.js_ファイルを開きます-
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('posts', function() {
this.route('new');
});
});
//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 'posts'}}Click Here{{/link-to}}
{{outlet}}//It is a general helper, where content from other pages
will appear inside this section
次のコードで_app/routes/_の下に作成されたファイル_posts.js_を開きます-
import Ember from 'ember';
export default Ember.Route.extend ({
templateName: 'posts/new'
});
次のコードで_app/templates/_の下に作成された_posts/new.hbs_ファイルを開きます-
<h2>Posts</h2>
Page is rendered by defining templateName property.
{{outlet}}
出力
エンバーサーバーを実行すると、次の出力が表示されます-
出力で受け取るリンクをクリックすると、次のスクリーンショットのような結果が生成されます-