dimanche 26 novembre 2017

Ember doesn't create nested models on store.createRecord

I have a nested model with default values. After I call this.get('store').createRecord('event') I try to get nested property in my form. But the only thing I can see is a null and an error in console because of 'can't get property country of undefined' for example. In this case I found workaround and created nested models manually like that. But I think it is not the best idea.

// the method to create event
           createEvent() {
                    let event = this.get('store').createRecord('event');
                    let location = this.get('store').createRecord('location');
                    let address = this.get('store').createRecord('address');
                    Ember.set(location, 'address', address);
                    Ember.set(event, 'location', location);
                    this.set('event', event);
                }

        // event model
        export default DS.Model.extend({
            '@type': DS.attr('string', {defaultValue: 'Event'}),
            'startDate': DS.attr('string', {defaultValue: tomorrow}),
            'endDate': DS.attr('string', {defaultValue: theDayAfterTomorrow}),
            'name': DS.attr('string'),
            'eventType': DS.attr('string'),
            'timeZone': DS.attr('string'),
            'location': DS.belongsTo('location', { async: false }),
        });

        // location model
        export default DS.Model.extend({
            '@type': DS.attr('string', {defaultValue: 'OnlineLocation'}),
            'name': DS.attr('string', {defaultValue: ''}),
            'url':  DS.attr('string', {defaultValue: 'http://'}),
            // 'address': DS.belongsTo('address', { async: false }),
            'address': DS.attr(),
            'location': DS.belongsTo('event')
        });
        // address model
        export default DS.Model.extend({
            '@type':    DS.attr('string', {defaultValue: 'Address'}),
            'country': DS.attr('string'),
            'locality': DS.attr('string'),
            'region': DS.attr('string'),
            'postalCode': DS.attr('number'),
            'streetAddress': DS.attr('string'),
            'location': DS.belongsTo('location', { async: false })
        });

     <!-- language: lang-js -->




Aucun commentaire:

Enregistrer un commentaire