0

I'm using Ember 2.4.2

app/models/product.js
export default DS.Model.extend({
    type: attr('string'),
    color: attr('string')
});

app/models/blue-bags.js
export default Product.extend({
    type: Ember.computed('color', function() {
        if (this.get('color') === 'blue') {
            return 'bag';
        }
        return '';
    })
});

The form that these properties are used in do show that type is set to 'bag' when the color is blue, but sending this data to the server doesn't (yes color was blue when sending data to the server).

Is this the correct way to overwrite properties of a model?

a7omiton
  • 1,597
  • 4
  • 34
  • 61
  • Only properties defined with `attr` get serialized, that's why the CP won't. You need a different approach here :\ – locks Oct 28 '16 at 07:08
  • 1
    @locks Thanks for the knowledge, I worked around it by adding an observer :) – a7omiton Oct 28 '16 at 10:01

1 Answers1

0

Solution was to create an observer in the model instead of overwriting the property, as @lock mentioned only properties defined with attr get passed to your serialiser (as it has a serialising method).

colorChanged: Ember.observer('color', function() {
  if (this.get('color') === 'blue') {
      this.set('product_type', 'bag');
  }
})
a7omiton
  • 1,597
  • 4
  • 34
  • 61