Backbone 动态保存 属性

Backbone Dynamically Save Property

我正在尝试将动态 属性 保存到 backbone 模型。

所以基本上 question 可以有一个 question_type 说 "car""house_type".

理想情况下,我想做这样的事情: @appuser.save(question.get('question_type'): answer.get('answer_number'))

其中 question.get('question_type') 动态转换为 @appuser.save(car: answer.get('answer_number'))@appuser.save(house_type: answer.get('answer_number'))

我试过 question_type = question.get('question_type') 然后 @appuser.save(question_type: answer.get('answer_number')) 但实际上只是为应用程序用户设置了 question_type

我如何在 Rails 中做到这一点的一个例子是做一个 appuser.update_attributes("#{question_type}" => value),它将动态地将 question_type 转换为 car 或者你有什么。

有什么想法吗?

编辑:

我已经能够将这些值存储在一个字符串中并将其保存到 question_type 并在我的服务器上适当地解析它。但我还是更愿意找到一种动态处理它的方法。

如果您查看 .savedocs,您会注意到您可以传递属性的散列。

由于您可以使用动态键创建对象,因此您可以先创建要设置的数据,然后将其传递给 .save

var attrs = {};
attrs[question.get('question_type')] = answer.get('answer_number');
@appuser.save(attrs);