@sorvah wrote:
A colleague and I have been thinking on a topic today and I’d love to get the feedback of other ember developers.
I have a webform that is rendered with a model. The model will either be saved or destroyed and has some hidden fields that render if certain inputs are valid. For example if ‘Other’ is selected in a down menu, a free text field will appear on the form.
This if logic is managed by an
{{#if property}}
block and the question we were pondering today is where it is valid to have thatproperty
.By default you would probably create the property on the controller. However, I realized that this form would be using two different controllers based on whether it was being rendered on a
/new
or/edit
route and I’d have to create them twice.So instead I added the property via a computed property within the model being worked on by the form.
This has a few benefits. It means that the model is always carrying the information it needs to render the form correctly as per the data within, whether it is used in
/new',
/edit` or anywhere else. It also means that the properties don’t have to be duplicated in multiple controllers. It works very well.But it does feel a bit wrong. It feels like it’s a level of state management that possibly shouldn’t be in the model as it’s working on a pure template concern. However, the properties are very specific to the model and will only be needed in cases where the model is being worked on in some respect.
You could argue that it isn’t that much different from the
fullname
example in the Guide docs - a computed property not returned to the API that is used to dictate some display behavior. Although the return offullname
is a piece of data, technically so is the return of theshow_other_field
as it’s just a boolean.So although it works (super well) it feels like I’m probably violating one of Embers core principles with this kind of implementation. I’d love to know what experienced Ember devs think and where the best location for these kinds of properties in.
Posts: 2
Participants: 2