I am building an App, where one can create surveys. Every survey has multiple questions. I am embedding the questions into the survey model (with embeds_many
in Mongoid), so a survey may look like this:
{
"id": "4f300a68115eed1ddf000004",
"title": "Example Survey",
"questions":
[
{
"id": "4f300a68115eed1ddf00000a",
"title": "Please describe your experience with backbone.js",
"type": "textarea"
},
{
"title": "Do you like it?",
"id": "4f300a68115eed1ddf00000b",
"type": "radiobutton",
"options": ["Yes", "Yes, a lot!"]
}
]
}
Now, there is also a survey editor which consists of a SurveyView
, which displays the survey and lists the questions. If I click on a single question, a QuestionView
will pop up, where I can edit the question. And when I am satisfied with my survey and I click save, the SurveyModel
will be sent to the server.
What is the best way to handle the embedded association?
If I pass survey.get("questions")[any_index]
to the QuestionView
, and the question gets changed, I have to manually search for the question.id
in my model and update my model. This feels wrong.
If I create a QuestionsCollection
in my SurveyModel
(is this even possible?). Then I can do things like fetching a Question
out of this collection by id, pass it to the view and when I change the model, everything will get updated automatically, but I have to specify an url
in the collection, and backbone will send single questions to the server, if things get updated.
Any suggestion on how to do this the backbone way?
Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.
BackboneJS is a lightweight JavaScript library that allows to develop and structure the client side applications that run in a web browser. It offers MVC framework which abstracts data into models, DOM into views and bind these two using events.
Model contains dynamic data and its logic. It performs various types of action on the data like validation, conversion, computed properties, access control etc. 1. It extends Backbone.
Backbone. js is a JavaScript framework that helps you organize your code. It is literally a backbone upon which you build your application. It doesn't provide widgets (like jQuery UI or Dojo).
I have implemented the answer from @Sander and just wanted to post some code:
class Survey extends Backbone.Model
# Handles the Survey.questions association to parse stuff from the server
parse: (resp) ->
if @attributes?.questions?
@attributes.questions.reset(resp.questions)
else
resp.questions = new QuestionsCollection(resp.questions)
resp
# Recollects Survey.questions
toJSON: ->
attributes = _.clone(@attributes)
attributes.questions = attributes.questions.toJSON()
attributes
Then I can do stuff like:
survey = Survey.get("my-id")
survey.questions.at(0).title = "First question"
survey.questions.at(1).title = "Second question"
survey.save()
Which works quite comfortable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With