lundi 11 décembre 2017

Ember component function outside of action function

I have a problem calling a function outside action functions from an actions function. As you can see from the code below, I have a selectClient action that calls two functions, createCompanyAccount and createPrivateAccount. But I always get a this.createPrivateAccount is undefined. I have tried using self, but to no avail. Weirdly, I thought I would have to use self.createCompanyAccount, but then I get a self.createCompanyAccount is not defined.

I use Ember 2.12 and 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');
        });
    }

});

Can anyone see where I go wrong? I can note that there is a few other functions that I have removed for clarity.

Thank you, Tommy




Aucun commentaire:

Enregistrer un commentaire