Creating a Survey App
Developing an application for creating surveys where each survey contains multiple questions. The questions are embedded into the survey model using embeds_many
in Mongoid, resulting in a survey structure like this:
{
"id": "4f300a68115eed1ddf000004",
"title": "Example Survey",
"questions":
[
{
"id": "4f300a68115eed1ddf00000a",
"title": "Share your experience with backbone.js",
"type": "textarea"
},
{
"title": "Do you enjoy it?",
"id": "4f300a68115eed1ddf00000b",
"type": "radiobutton",
"options": ["Yes", "Yes, a lot!"]
}
]
}
The survey editor includes a SurveyView
to display the survey and list the questions. Clicking on a question launches a QuestionView
for editing that specific question. Upon saving, the updated SurveyModel
is sent to the server.
Dealing with Embedded Associations
Exploring the best approach to handle the embedded association:
One method involves passing
survey.get("questions")[any_index]
to the QuestionView
. However, if the question is edited, manual search for the question.id
in the model is required, which can be inefficient.
Alternatively, creating a QuestionsCollection
within the SurveyModel
allows for fetching a Question
by id from the collection. This approach ensures automatic updates when the model is changed, although specifying a URL in the collection might result in unnecessary server requests for individual questions updates.
Seeking suggestions on how to handle this situation in accordance with the backbone framework.