我正在开发一个需要不同视图的应用程序,具体取决于用户角色。最初,我设法使用包含 ajax 请求操作和模型的单独文件夹将我与 API 的交互分开,以避免在 VM 中重复代码。
app/controllers/
- 包含每个对象的GET
,POST
,PUT
,操作和模型DELETE
然后我有了应用程序其余部分的结构:
app/building/ [route:building/]
- shell.html
- shell.js
- /offices (list of offices)
- index.html
- index.js
- /equipment (list of equipments)
- index.html
- index.js
app/offices/ [route:offices/:id]
- shell.html
- shell.js
- /employees (list of employees)
- index.html
- index.js
- /details (office details)
- index.html
- index.js
app/employees/ [route:employees/:id]
- shell.html
- shell.js
- /report (individual employee data)
- index.html
- index.js
- /details (empoyee details)
- index.html
- index.js
这将是管理员角色。如您所见,有很多外壳、子外壳、子路由器和路由。并且需要进行更改,因为并非所有角色都可以访问相同的路由。
起初,我尝试了 viewURL 方法,我在会话开始时保存角色并使用 提供适当的视图viewUrl
,但后来我注意到文件变得过于臃肿:
- 必须对主路由器和子路由器进行更改,并删除低级别用户声明的路由
- 每个页面的单独视图(admin.html、employee.html)都共享相同的视图模型,其中包含员工永远不会使用的功能
然后我想出了另一个结构。和以前一样,只是每个角色都有自己的文件夹:
app/roles/admin/
- building/
- ...
- offices/
- ...
- employees/
- ...
app/roles/employee
- building/
- ...
- offices/
- ...
- employees/
- ...
当应用程序加载时,我只需根据角色设置根目录就可以了。
优点
- 更易于单独维护
- 除了应用程序启动之外没有角色检查
- 角色特定的代码,没有为其他角色搞砸的风险
缺点
- 更多文件
- 作为一个群体更难维持。例如,如果我重写一个在所有角色之间共享的函数,我必须为每个角色替换该代码
我对第二种结构感觉更舒服,但我想知道你们的想法。或者,如果您有更好的角色处理方式。