Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render template based on object type #2491

Closed
nkosi23 opened this issue Jul 20, 2019 · 5 comments
Closed

Render template based on object type #2491

nkosi23 opened this issue Jul 20, 2019 · 5 comments

Comments

@nkosi23
Copy link

nkosi23 commented Jul 20, 2019

Hello,

I am implementing a questionary and I am looking for an advice regarding the implementation. There are multiple question type: some require the user to type answer, others to select radio buttons and so on.

What I'd like is to have a different javascript class for each type of question, and have knockout render each question object using a template chosen automatically based on the object type.

What I am asking for is general advice regarding the best way to implement this. Right now my idea is to use the foreach binding and withing this loop, use if bindings to check the object type.

However I wondering if there is an elegant way to solve this problem using components or something similar. I'd like to make it reusable and to encapsulate this logic as much as possible.

Thank you

@nkosi23
Copy link
Author

nkosi23 commented Jul 20, 2019

Okay, I think what I am after is a way to choose a template based on a parameter of the viewModel, however in the component handler, the template function does not seem to have access to the state of the viewModel function.

My understanding is that what I'd need to do is implement a custom componentLoader to register a custom loadTemplate function, so the question is: how can I add custom values to the templateConfig property of the loadTemplate function?

https://knockoutjs.com/documentation/component-binding.html

https://knockoutjs.com/documentation/component-loaders.html

@avickers
Copy link

https://knockoutjs.com/documentation/template-binding.html

Scroll down to Note 5.

@bludev
Copy link

bludev commented Jul 22, 2019

I think the encapsulation of the various types of question/answer is ideal but not very useful, as there is no real complexity for each type.
A simple example: https://codepen.io/bludev/pen/aevYrN

@chrisknoll
Copy link

We do something similar to what you are asking (if I am interpreting your use case correct):

We have a component-per-type arragement, such that we get the component name to use for the given type, seen here:

https://github.com/OHDSI/Atlas/blob/master/js/components/cohortbuilder/components/CohortExpressionEditor.js#L361

You register a ko component with a given name that matches the given type. You pass the data as a param value to the component, and the component is responsible for providing the UI element to modify the type.

Example of one of the components:
Template: https://github.com/OHDSI/Atlas/blob/master/js/components/cohortbuilder/components/ConditionOccurrenceTemplate.html
ViewModel: https://github.com/OHDSI/Atlas/blob/master/js/components/cohortbuilder/components/ConditionOccurrence.js

@nkosi23
Copy link
Author

nkosi23 commented Jul 23, 2019

Thank you all, this was massively helpful! @avickers perfectly addressed my question but @BlueDev's remark was very spot on. While I will not go with your approach on that one @chrisknoll thank you so much for sharing it, it was very inspiring.

Thank you very much

@nkosi23 nkosi23 closed this as completed Jul 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants