动作功能之外的灰烬分量功能

时间:2017-12-11 10:40:55

标签: ember.js

我在从动作函数调用动作函数之外的函数时遇到问题。从下面的代码中可以看出,我有一个selectClient操作,可以调用两个函数createCompanyAccountcreatePrivateAccount。但我总是得到this.createPrivateAccount is undefined。我曾尝试使用self,但无济于事。很奇怪,我以为我必须使用self.createCompanyAccount,但后来我得到self.createCompanyAccount is not defined

我使用Ember 2.12和Ember Data 2.16.3。

import Ember from 'ember';

export default Ember.Component.extend({
    store: Ember.inject.service(),
    tagName: '',

    /**
     * Actions
     */
    actions: {
        // Select from selectList
        selectClient(element) {
            let self = this;

            if (element.company) {
                this.get('store').query('account', { 'filter' : {'orgnumber': element.orgNumber}}).then(
                    (accounts) => {
                        /* Organisation exist already */
                    },
                    (error) => {
                        let code = Number(error.errors[0].status);
                        if (code === 404) {
                            // company does not exist, so lets create it, and an account.
                            this.createCompanyAccount(element).then(
                                (account) => {
                                    /* Do stuff... */
                                }
                            );
                        }
                    }
                );
            } else {

                this.createPrivateAccount(element).then(
                    (anonUser) => {
                        /* Do stuff... */
                    }
                );

            }
        }
    },

    createCompanyAccount(company) {
        let account = this.get('store').createRecord('account', {
            type: 'company',
        });

        // Check if postal address is set on result
        if (typeof company.addressObject !== 'undefined') {
            let postAddress = this.get('store').createRecord('address', {
                address: company.addressObject.streetName,
                zip: company.addressObject.zip,
                postal_address: company.addressObject.postalAddress
            });
            account.get('addresses').pushObject(postAddress);
        }

        this.get('store').createRecord('company', {
            name: company.name,
            org_number: Number(company.orgNumber),
            account: account
        }).save().then((new_company) => {
            return new_company.get('account');
        });

    },

    createPrivateAccount(person) {
        let account = this.get('store').createRecord('account', {
            type: 'anonuser'
        });

        // Check if postal address is set on result
        if (typeof person.addressObject !== 'undefined') {
            let postAddress = this.get('store').createRecord('address', {
                address: person.addressObject.streetName,
                zip: person.addressObject.zip,
                postal_address: person.addressObject.postalAddress
            });
            account.get('addresses').pushObject(postAddress);
        }

        this.get('store').createRecord('anonUser', {
            first_name: person.firstName,
            sur_name: person.surName,
            email: person.email,
            phone: person.phone,
            account: account,
        }).save().then((new_person) => {
            return new_person.get('account');
        });
    }

});

谁能看到我哪里出错了?我可以注意到,为清楚起见,我删除了一些其他功能。

谢谢你, 托米

1 个答案:

答案 0 :(得分:0)

您的问题与error "Keynote got an error: every text item of slide 1 of document 1 doesn’t understand the “select” message." number -1708 from every text item of slide 1 of document 1 this.createPrivateAccount未定义无关。我认为它们都被执行但是它们确实返回this.createCompanyAccount但你期待undefined。因此Promise未定义。