View
クラスでプロパティを作成したため、場合によってはキーワードのview
ようなポインタのようなプロパティを使用する必要があります。this
テンプレートを次のように変更します。
<script type="text/x-handlebars">
{{#view App.AView}}
{{view.firstName}}
{{view.surname}}
{{/view}}
</script>
Ember の規則により、コードの両方のハンドルバー テンプレートは同じものを表します。名前がない場合、Ember はテンプレート名を「アプリケーション」と見なします。これは、プロパティを{{view.propertyName}}
両方のテンプレートに修正したとしても、Ember がテンプレート (ハンドルバーを含む) をテンプレート関数にコンパイルし、名前がテンプレートのコレクション ( path Ember.TEMPLATES
) へのキーとして使用されるため、上記のコードのように、これらの式を単一のテンプレートに移動する必要があります。
ただし、このようなビューの使用は避ける必要があります。
ビューはデータを表示する必要がありますが、データを保持するべきではありません。データはモデル (ストア内) に存在する必要があり、ビューはコントローラーにデータを要求し、それを取得する必要があります。コントローラーには、ルーターを介してストアからデータを入力する必要があります (コントローラーは、何をいつ実行するかを認識しています)。
これは悪い意味で言っているのではありません。Ember を使い始めて、間違った側を運転するのを防ごうとしているだけです。
多くの古いチュートリアルがあり、これが時々多くの混乱を引き起こすことはよく知られています (更新または免責事項を追加するための通知を受け取る必要がある、古いチュートリアル/記事を含むレポがあります)。しかし、一般的には、ガイドに従うこと、 Ember に関するビデオを見ること、インターネットで利用可能な他の リソース を確認することをお勧めします。
以下は、非常に基本的なサンプル アプリケーションのコメント付きコードで、使用できる機能と使用すべき機能の一部を示しています。
ハンドルバー:
<!--
when a template doesn't have a data-template-name, Ember assumes this is the
application main template. This is usually where you'd render the layout structure
and also where you'd put the main outlet
-->
<script type="text/x-handlebars">
<h1>Example</h1>
{{outlet}}
</script>
<!--
As per convention, a named template should match with its route name
There are ways around using "partial", "render", or even defining
a View class and setting the templateName property to a different name, or
using the route's renderTemplate hook
Another thing. You can have nested views when using nested routes
This view template has another outlet to display a person from the collection
-->
<script type="text/x-handlebars" data-template-name="people">
{{#each person in controller}}
{{#linkTo people.person person}}
{{person.fullName}}
{{/linkTo}}<br />
{{/each}}
<hr />
{{outlet}}
</script>
<!--
Unlike the very first code piece in this answer, when you have a view or
template connected to a controller, you can access the data from the controller
using handlebars expressions.
-->
<script type="text/x-handlebars" data-template-name="people/person">
First name: {{view Ember.TextField valueBinding="firstName"}}<br />
Last name: {{view Ember.TextField valueBinding="lastName"}}<br />
Full Name: {{fullName}}
</script>
JavaScript :
window.App = Ember.Application.create();
// defining routes which are somewhat like states (think of a state machine)
// they also provide the ability to have hash urls
// the router is a very important piece of ember due to conventions
App.Router.map(function() {
// sample url ~/#/people
this.resource('people', function() {
// sample url ~/#/people/1
this.route('person', { path: ':person_id' });
});
});
// In this route we provide the data to the list view in "people" template
// the data will actually go to the controller 'content' property which can
// be a type of array for arraycontroller or a single object for object controller
// this should allow the view to call data from the controller
App.PeopleRoute = Em.Route.extend({
model: function() {
return App.Person.find()
}
});
// in this route we provide data for the "people/person" template
// In this case we are using the person id from the parameters to query our
// application store.
App.PeoplePersonRoute = Em.Route.extend({
model: function(params) {
return App.Person.find(params.person_id)
}
});
// This is the very first route of the application
// Most of the time, you'll simply redirect from your index to a resource
// in this example, from ~/#/ to ~/#/people
App.IndexRoute = Em.Route.extend({
redirect: function() {
this.transitionTo('people');
}
});
// The store manages your application data. Normally you only have to define
// the revision since it's not 1.0 yet (https://github.com/emberjs/data/blob/master/BREAKING_CHANGES.md)
// for this sample, I'm using the Fixture Adapter so I can add mock up data to the
// app while testing/coding front end
App.Store = DS.Store.extend({
revision: 11,
adapter: 'DS.FixtureAdapter'
});
// Using Ember-Data, you can define a Model object which uses application
// semantics to describe your data, and does many operations which you'd
// normally expect to see in a ORM. Ember-Data is no ORM, but it gets pretty close
// and in certain scenarios it goes beyond
App.Person = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
fullName: function() {
return '%@ %@'.fmt(
this.get('firstName'),
this.get('lastName')
);
}.property('firstName', 'lastName')
});
// Using the FixtureAdapter you can add mockup data to your data store
App.Person.FIXTURES = [
{id: 1, firstName: 'Joe', lastName: 'Bloggs'},
{id: 2, firstName: 'Other', lastName: 'Dude'}
];
// when your controller wants to handle a collection, use ArrayController
App.PeopleController = Em.ArrayController.extend();
// when it handles a single object, use ObjectController
App.PeoplePersonController = Em.ObjectController.extend();