I'm attempting to save a dynamic property to a backbone model.
So basically a question
could have a question_type of say "car"
or "house_type"
.
Ideally I'd like to do something like this:
@appuser.save(question.get('question_type'): answer.get('answer_number'))
Where question.get('question_type')
dynamically shifts to @appuser.save(car: answer.get('answer_number'))
or @appuser.save(house_type: answer.get('answer_number'))
I tried doing question_type = question.get('question_type')
and then @appuser.save(question_type: answer.get('answer_number'))
but that actually just sets question_type
for the appuser.
An example of how I would do this in Rails would be to do a appuser.update_attributes("#{question_type}" => value)
which would dynamically convert question_type to car
or what have you.
Any ideas?
EDIT:
I've been able to just store these values in a string and save that to question_type
and parse it out appropriately on my server. But I'd still prefer to find a way to handle it dynamically.
If you look at the docs for .save
you'll notice that you can pass a hash of attributes.
Since you can create objects with dynamic keys you can first create the data you want to set and then pass it to .save
var attrs = {};
attrs[question.get('question_type')] = answer.get('answer_number');
@appuser.save(attrs);