2016-06-27 2 views
0

私はnew-thingのルートを持っており、そのテンプレートはフォームです。このフォームが送信されると、私はアクションを呼び出して、thisを渡します。私のテンプレートでEmberでの保存に失敗した場合のエラーの表示

export default Ember.Route.extend({ 
    model() { 
    return {}; 
    }, 

    actions: { 
    createThing(thing) { 
     let thingToSave = this.store.createRecord('thing', { 
     name: thing.name, 
     description: thing.description 
     }); 

     thingToSave.save().then(function() { 
     // happy path 
     }).catch(function(reason) { 
     // unhappy path 
     }); 
    } 
    } 
}); 

、私は次のようにあります

{{#each model.errors.name as |error|}} 
    <div class="error"> 
    {{error.message}} 
    </div> 
{{/each}} 

しかし、何かがかなり右に配線されていません。私は経路のモデルがどのように終わっているのかはっきりしていないthingbad pathがトリガーしたときにエラーがレンダリングされるように保存しようとしています。

答えて

3

あなたmodel()空白のレコードを作成する必要があります

model() { 
    return this.store.createRecord('thing'); 
} 

あなたのモデルオブジェクトにフォームフィールドをバインドします。

次に、あなたがルートに保存し、HTTPリクエストの結果に応えることができます。

actions: { 
    createThing() { 
    this.get('model').save().then((thing) => { 
     // happy path 
    }, 
    (err) => { 
     // unhappy path 
    }) 
    } 
} 
+0

Haが、私はちょうどこれを思い付くていました。私が別にやっているのは 'this.get( 'model')'の代わりに 'let this = this.modelFor(this.routeName);'だけです。ありがとう〜 – Gregg

+1

あなたは大歓迎です。私のための素晴らしいリファレンスは、101年前のことです:https://leanpub.com/ember-cli-101私はこの質問への回答をかなり早く検索するためにそれを使用しました。 –

関連する問題