doupingtang9627 2013-05-11 03:54
浏览 20

MVC大规模版本视图的抽象级别

I have an MVC that serves a form to a view. I'm pondering the best approach to serving form content, that is form elements such as labels and form inputs such as selects. It is possible that the form elements will change often and will have many different versions spanning several pages.

I have thought about three approaches, each unsatisfactory to me.

  • Each version of the form will be fully written in html and saved as a discreet view.

Pros: If I need a particular set of elements, I can call a view with one line of code from my controller.

Cons: I will end up with dozens upon dozens of views.

  • Shift the load to my controller, and pass an array of element names to my view array('select' => array('label' => "element 1", 'class => "big"), 'select', 'input', 'input'), which will take the array and parse it into the markup as form elements.

Pros: Allows me to have just one view, and I can specify the set of form elements I want loaded into the view at the controller level.

Cons: My controller will go from several lines of code to several hundred lines of array definitions pretty quick.

  • Serialize sets of elements and tuck them away into a database to be called whenever.

Pros: Several lines of code at my controller and one view file.

Cons: Making changes to any one set of form elements becomes a real pain. Anytime I have to include a new set or make changes, I have to access the database and unserialize and serialize.

So for templating different versions of the same form, and also allowing interchangeability of form elements, what is the approach do you guys suggest?

  • 写回答

1条回答 默认 最新

  • dongyi7966 2013-05-11 04:40
    关注

    You could build a library of form methods and extend the appropriate controllers. I will usually build libraries or "helper" classes for any perpetual data, keeping my controllers uniform and tidy. Doing so gives you the ability to simply extend a controller to class Form_Data where you could have dedicated methods that build individual form elements, also enforcing separation of concerns.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP