You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Anyone have any good patterns for sending classes over the SSR boundary, or working with the objects you can send over?
When sending something from server-side to client-side, it get's stringified (with @nuxt/devalue), losing all the class info (methods, equivalence, etc) in the process.
I've started adding static methods to all my classes to do things that take the "plain" object and do stuff to it.
An example
exportclassProduct{constructor(obj){Object.assign(this,obj);// Post process the data from APIthis.price=this.price ? parseFloat(this.price) : undefined;// price is a string :c_assert(!Number.isNaN(this.price),`Price '${obj.price}' should parse as a float or fail`);}staticformatPrice(product){if(product===undefined||product===null){return'';}return`$${product.price.toFixed(2)}`;}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Anyone have any good patterns for sending classes over the SSR boundary, or working with the objects you can send over?
When sending something from server-side to client-side, it get's stringified (with
@nuxt/devalue
), losing all the class info (methods, equivalence, etc) in the process.I've started adding static methods to all my classes to do things that take the "plain" object and do stuff to it.
An example
How do you normally handle this?
Beta Was this translation helpful? Give feedback.
All reactions