2016-10-15 8 views
1

ので、私はそうのようなコントローラからストアにアクセスしようとしています:Ember.js - コントローラから店舗を正しく呼び出すにはどうすればいいですか?

import Ember from 'ember'; 

export default Ember.Controller.extend({ 
    emailAddress: '', 
    message: '', 

    isValidEmail: Ember.computed.match('emailAddress', /^[email protected]+\..+$/), 
    isMessageLongEnough: Ember.computed.gte('message.length', 10), 

    isValid: Ember.computed.and('isValidEmail', 'isMessageLongEnough'), 
    isNotValid: Ember.computed.not('isValid'), 

    actions: { 

    sendConfirmation() { 
     this.store.createRecord('contact', { 
     email: emailAddress, 
     message: message, 
     }).save(); 

     this.set('responseMessage', 'We got your message and we will be in contact soon :)'); 
     this.set('emailAddress', ''); 
     this.set('message', ''); 
    } 
    } 

}); 

、私はEmber.js 2.7のドキュメントを見て、1店舗へのアクセス権を持つことができる場所、それは特にあなたに教えてくれありませんしかし、私はそれがコントローラやルートを介してアクセスできることを知っています。このようにそれをやってしかし

は、私にこれらのエラーを与える:

controllers/contact.js: line 17, col 16, 'emailAddress' is not defined. 
controllers/contact.js: line 18, col 18, 'message' is not defined. 

私はそれが私がコントローラ、または私はEMAILADDRESSとメッセージを定義した方法をアクセスしていますが方法だかはわかりません。

ご協力いただきありがとうございます。

解決しよう:それはこのことをされている必要があります

sendConfirmation() { 
    this.store.createRecord('contact', { 
    email: emailAddress, 
    message: message, 
}).save(); 

sendConfirmation() { 
    this.store.createRecord('contact', { 
    email: this.get('emailAddress'), 
    message: this.get('message'), 
    }).save(); 

:)

答えて

0

storeは、controllerrouteに注入されます。もう1つはプロパティを取得する必要がありますget

sendConfirmation() { 
    var newRecordObj = {}; 
    newRecordObj['email'] = this.get('emailAddress'); 
    newRecordObj['message'] = this.get('message'); 

    this.get('store').createRecord('contact', newRecordObj).save((result) => { 
     //success handling 
     this.set('responseMessage', 'We got your message and we will be in contact soon :)'); 
     this.set('emailAddress', ''); 
     this.set('message', ''); 
    },() => { 
     //error handling 
     this.set('responseMessage', 'Error message'); 
    }); 
} 
1

あなたの問題は、あなたが店にアクセスする方法はありませんが、それはあることだ この部分について実際に変数を定義することなく、連絡先に電子メールとメッセージを追加しようとしています。

sendConfirmation() { 
    this.store.createRecord('contact', { 
    // what do you expect emailAddress and message values to be at this point? 
    email: emailAddress, // <-- emailAddress is not defined 
    message: message, // <-- message is not defined 
    }).save(); 
    // ... 

おそらく最初に検索することを意味しましたか?

sendConfirmation() { 
    // retrieve emailAddress and message first 
    const { 
    emailAddress, 
    message 
    } = this.getProperties('emailAddress', 'message'); 

    // then use them to create a contact 
    this.store.createRecord('contact', { 
    email: emailAddress 
    message: message 
    }).save(); 
    // ... 

もう一つは、おそらくゲッター/セッターを使用しているので、this.get('store')を使用して行われるべきストアにアクセスするプロパティを操作/アクセスの燃えさし-方法です。

関連する問題