2016-10-15 3 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 и конкретно не скажу вам, где можно получить доступ к магазину, но я знаю, что он может получить доступ к нему через контроллер или маршрут.

Однако, делая это таким образом, дает мне эти ошибки:

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 будет вводиться в controller и route. и еще одна вещь, вы должны получить свойства через 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'), так как с помощью геттеров/сеттеров является уголек-способом доступа/манипулирования свойств.

Смежные вопросы