であると思うので、両方を学ぶためにemberとhapiを使用するアプリを作り始めました。Emberアプリはhapijsを使用していないAPIを使用しています。私のAPIはちょうどEmberの問題
私は以下のスニペットを貼り付けましたが、あなたが持っているものを見ることに興味があるならば、私はサーバとemberアプリの両方をここでレポで利用できます。https://github.com/jrutishauser/contactsApp
私は
http://localhost:3000/api/contacts
で私のSQL Server
{"data":[{"id":1,"first_name":"Joe","middle_initial":null,"last_name":"Smith","title":null,"phone_number":"(555) 111-2222","email":"[email protected]","street_address":"123 Some Street","city":"Salt Lake City","state":"Utah","zip_code":"84111","created_at":1456988902625,"updated_at":1456988902625},{"id":2,"first_name":"John","middle_initial":null,"last_name":"Jones","title":null,"phone_number":"(555) 222-3333","email":"[email protected]","street_address":"1432 Another Street","city":"Montgomery","state":"Alabama","zip_code":"99291","created_at":1456988902625,"updated_at":1456988902625},{"id":3,"first_name":"Joe","middle_initial":null,"last_name":"Smith","title":null,"phone_number":"(555) 111-2222","email":"[email protected]","street_address":"123 Some Street","city":"Salt Lake City","state":"Utah","zip_code":"84111","created_at":1457588539468,"updated_at":1457588539468},{"id":4,"first_name":"John","middle_initial":null,"last_name":"Jones","title":null,"phone_number":"(555) 222-3333","email":"[email protected]","street_address":"1432 Another Street","city":"Montgomery","state":"Alabama","zip_code":"99291","created_at":1457588539468,"updated_at":1457588539468}]}
から以下を示す私のHAPIサーバは、私の燃えさしアプリは、
アダプター/アプリケーションを含む下記にこれらのファイルを持っています。 js、
1 import DS from 'ember-data';
3 export default DS.JSONAPIAdapter.extend({
4 namespace: 'api',
5 host: 'http://localhost:3000'
6 });
モデル/ contact.js、
1 import DS from 'ember-data';
3 export default DS.Model.extend({
5 firstName: DS.attr('string'),
6 lastName: DS.attr('string'),
7 middleInitial: DS.attr('string'),
8 title: DS.attr('string'),
9 phoneNumber: DS.attr('string'),
10 email: DS.attr('string'),
11 streetAddress: DS.attr('string'),
12 city: DS.attr('string'),
13 state: DS.attr('string'),
14 zipCode: DS.attr('string'),
15 createdAt: DS.attr('date'),
16 updatedAt: DS.attr('date')
17 });
ルート/ contacts.js
1 import Ember from 'ember';
2
3 export default Ember.Route.extend({
4 model: function(){
5 return this.store.findAll('contact');
6 }
7 });
私もシリアライザ/ application.jsの下にこれを設定するが、私は正常化する必要がいけないので、その必要と思ういけません何
1 import DS from 'ember-data';
2
3 export default DS.JSONAPISerializer.extend({
4 primaryKey: 'id',
5 serializeId: function(id) {
6 return id.toString();
7 }
8 });
テンプレート/ contacts.hbs
1 <ul>
2 {{#each contact in model}}
3 <li>{{#link-to 'contact' contact}}{{contact.firstName}}{{/link-to}}</li>
4 {{/each}}
5 </ul>
6
7 {{outlet}}
すべて私は期待している連絡先ルートは現在のところAPIからのリストを表示するだけですが、私はまだこれで終わっていないし、しばらくの間ここで立ち往生しています。
私がされている取得コンソールエラーは、
ember.debug.js:28590 Error while processing route: contacts.index Assertion Failed: You may not set `id` as an attribute on your model. Please remove any lines that look like: `id: DS.attr('<type>')` from (unknown mixin) Error: Assertion Failed: You may not set `id` as an attribute on your model. Please remove any lines that look like: `id: DS.attr('<type>')` from (unknown mixin)
と
Error: Assertion Failed: Encountered a resource object with an undefined type (resolved resource using [email protected]:application:)
これらのリソースを共有してくれてありがとう、私は今日チャンスを取って、うまくいけば私の間違いを見つけるときにそれらを通過します。 – imnothardcore
@imnothardcore例2番目のエラーは、仕様でモデルの型を定義する必要があるためです。だから、それぞれの連絡先は 'type = 'contact''というレスポンスでなければなりません。また、すべての属性はレスポンスなどで 'attributes'ハッシュの中で定義する必要があります。上記のEmberガイドからのリンクでは、ペイロードの見た目を少し良く見せています。 – Altrim
ありがとう、それは私のすべてのコンソールエラーを解決し、emberクロムプラグインを使用して私は4つのオブジェクトで私の配列を見ることができますが表示されます。私のemberモデル/テンプレートにエラーがあるように見えます – imnothardcore