Model computed property to json

Hi people,

I compile some handlebars during runtime and want to send JSON data in. What I need is serialized computed properties of a model. For example I have “quantity” and “price” properties and computed property “amount”. Serialization gives me JSON with just “quantity” and “price”

Suggestions ? Thank you

Hi acero, I guess you could override serialize in your model-specific or application serializer.

Off the top of my head, something like:

export default DS.JSONAPISerializer.extend({

  serialize(snapshot) {
    const json = this._super(...arguments);
    // add property from snapshot to json where appropriate
    return json;
  }

});

Hi emberigniter, thank you for idea I will try it, but I would actually like to avoid serialize method, so that I do not send unnecessary properties to server, ideas?

Additional question; how to list or loop through computed properties?

That’s the exact opposite: we use the serialize method to control exactly which properties we send to the server.

By the way, I think that snapshot only contains DS.attr attributes; in that case you can access the record itself by calling snapshot.record.get('computedProperty')

1 Like

ok, I think I understand your usage of serialize, but I need one “serialize” to comunicate to api server (without computed properties, this works ok as it is) and other method like toJson (but with computed prop.) to feed runtime compiled template completely on client side. I will try record.get('computedProperty') thnx, but is there a way to loop through all computed properties?

Sorry, I am not aware of how to loop through computed properties.