Hacker News new | ask | show | jobs
by condiment 4844 days ago
Correct me if I'm wrong, but wouldn't the best place to combine and transform model data be in the model itself?

For trivial cases, this could be implemented in the model's 'defaults' function. (by computing the transformed field value in the function and returning a hash) If you forsee the need to update the value of the dynamic field in response to the client updating the model, you can perform the calculation again in the Model's 'validate' function.

Speaking from experience, logic in templates is convenient for trivial use cases, but can be very difficult to maintain once a project grows.

2 comments

"logic in templates is convenient for trivial use cases, but can be very difficult to maintain once a project grows"

Agreed, my first thought was that there is no way to actively debug the template logic.

Exactly. We have a generic serialize() method in our models to do common toJSON. In item views, where performance is critical, the view itself transforms a subset of the models properties to JSON.