tarponjargon
tarponjargon

Reputation: 1032

ember: copy model data to another

I have a basic order entry form with billing and shipping address fields (both based on an 'address' model). I have a checkbox that says "Billing address same as shipping?" that, when checked, will copy the billing address data to the shipping address.

enter image description here

How would I do this? It's not quite apparent to me. I'm thinking that when the "next" button is clicked, if the "billShipSame" value = true then copy the data. But how do you actually copy the data? Or am I just approaching this problem wrong?

The model looks like:

export default DS.Model.extend(Validations, {
    type: attr('string'),
    firstName: attr('string'),
    lastName: attr('string'),
    address1: attr('string'),
    address2: attr('string'),
    city: attr('string'),
    state: attr('string'),
    country: attr('string'),
    postalCode: attr('string'),
    phone: attr('string')
});

And here's a stripped-down version of how I'm using them:

billingAddress: computed(function() {
    return this.get('store').createRecord('address', { type: 'billing'});
}),

shippingAddress: computed(function() {
    return this.get('store').createRecord('address', { type: 'shipping'});      
}),

orderModel: computed(function() {
    return this.get('store').createRecord('order', {
        billingAddress: this.get('billingAddress'),
        shippingAddress: this.get('shippingAddress')
    });
}),

Upvotes: 0

Views: 494

Answers (1)

acorncom
acorncom

Reputation: 5955

I would suggest having you "same as billing" radio button trigger an action that copies the data into the appropriate fields. That way by the time someone clicks next, your data model is in good shape and your submit action can focus on saving

Edit:

These easiest way to copy values between two models is as follows:

shippingAddress.setProperties(billingAddress.getProperties('firstName','lastName')); // etc

Believe that should handle what you're after ...

Upvotes: 1

Related Questions