[英]Multimple forms for one model backbone.js
The question is about managing models in forms on client with backbone.js 问题是关于使用骨干.js在客户端上以表格形式管理模型
There is a model User on the server. 服务器上有一个模型用户。 It contains various fields:
它包含多个字段:
I have a single page application of settings. 我有一个设置的单页应用程序。 There are two forms on it: "personal info" form and "other info" form.
它有两种形式:“个人信息”形式和“其他信息”形式。 I although have an api, that contains two routes to handle it -
/api/user/<id>/personal_info
, /api/user/<id>/other_info
(it could be changed, it does not matter). 我虽然有一个api,但包含两个处理它的路由-
/api/user/<id>/personal_info
, /api/user/<id>/other_info
(可以更改,没关系)。 I can PUT or GET info from these apis. 我可以从这些API放置或获取信息。
So I can't decide how to organize my backbone models right. 因此,我无法决定如何正确组织骨干模型。 Right now I have two models - UserPersonalModel and UserOtherModel, each of them has it's own api and I save them apart from each other.
现在,我有两个模型-UserPersonalModel和UserOtherModel,每个模型都有自己的api,我将它们彼此分开保存。
Am I doing right or I shall rewrite it to one js model UserModel and call different save methods like .savePersonal and .saveOther? 我做对了还是将其重写为一个js模型UserModel并调用了不同的保存方法,例如.savePersonal和.saveOther? What is the best practice?
最佳做法是什么?
Unless you're displaying multiple user forms at the same time and you'll have to do extra work mapping each other_info model with respective personal_info, I don't see any reason to combine those two as single model. 除非您同时显示多个用户表单,并且您必须做额外的工作才能将贴图other_info模型与各自的personal_info映射,否则我没有理由将这两个模型合并为一个模型。
Especially since you have two endpoints, it's easier to have them as separate models, you can have separate view controlling each forms with respective models as well. 尤其是由于您有两个端点,将它们作为单独的模型会更容易,因此您也可以使用单独的视图来使用各自的模型来控制每个表单。
Combining the models will probably create nested attributes, then you'll have to manage that as well (Using plugins like deep model). 组合模型可能会创建嵌套的属性,然后您也必须对其进行管理(使用深度模型之类的插件)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.