简体   繁体   English

混合使用Javascript和HTML与可维护的代码

[英]Mixing Javascript and HTML vs Maintainable code

I have a form with 100+ form controls. 我有一个包含100多个表单控件的表单。 My problem is that the form requires non-trivial logic to determine which form controls to hide, show, disable, etc. Right now I have several if-then-else structures in several event handlers that are a nightmare to maintain/update. 我的问题是,表单需要非平凡的逻辑来确定要隐藏,显示,禁用等的表单控件。现在,我在多个事件处理程序中具有多个if-then-else结构,这是维护/更新的噩梦。 I have been dabbling in AngularJS and thought I might be able to use something like ng-hide in my JQuery based form. 我一直在研究AngularJS,并认为我可以在基于JQuery的表单中使用ng-hide之类的东西。 Just to be clear I don't want to switch to AngularJS. 为了清楚起见,我不想切换到AngularJS。

My main questions are: Is this a safe way to use eval() ? 我的主要问题是:这是使用eval()的安全方法吗? Is there a better way to achieve my goal, while keeping maintainability in mind? 在牢记可维护性的同时,有没有更好的方法可以实现我的目标?

HTML 的HTML

 <button type="button" ng-show="_isRefill" value="">Test1</button>
 <button type="button" ng-show="_hasChanges || _isRefill" value="">Test2</button>

JQuery jQuery查询

function test()
{

  var _isRefill = true;
  var _hasChanges = false;  

  //There about 10 of these Booleans that control what to show/hide/etc
  //Each DOM element could have it's own unique combination of 10 Booleans that control it  
  //Note that Button #2 has logic in the HTML markup 

  var $this = $(this);

  if( !eval($this.attr('ng-show')) )
    $this.addClass('ignore');
  else 
    $this.removeClass('ignore');

}

$('button').on('click',test);

The code inside the ng-show attribute is echoed from the server so I don't think I will have an XSS problem, but I could be wrong. ng-show属性中的代码从服务器回显,所以我认为我不会遇到XSS问题,但是我可能是错的。

I have considered using something like the observer pattern, but I don't want to create and keep track of a ton of IDs. 我曾考虑使用类似观察者模式的方法,但是我不想创建并跟踪大量ID。 Also, a lot of my DOM elements are dynamically generated which would make maintaining the subject lists a pain. 而且,我的许多DOM元素都是动态生成的,这会使维护主题列表感到很痛苦。

Edit 编辑

Here is an example use case of the form. 这是表单的示例用例。 I took a screenshot of the same section of the form at different states. 我在不同状态下截取了表单同一部分的屏幕截图。

Case 1 The radio buttons labeled by "Yes, This is a waiver" and "No, This is not a waiver" determine whether "Waiver Email" and "Business Case" are shown. 情况1分别标记为“是,这是放弃”和“否,这不是放弃”的单选按钮决定是否显示“放弃电子邮件”和“业务案例”。

Case 2 User selects Refill, With Changes, Temporary(TMP) and then is allowed to choose the options from case 1 to show/hide the Waiver section. 情况2用户选择“带更改,临时的(TMP)补充”,然后被允许从情况1中选择选项以显示/隐藏“放弃”部分。

Case 3 User selects New Position, Intermittent and the Waiver section is forced to Yes and locked. 情况3用户选择“新位置,间歇”,“放弃”部分被强制为“是”并被锁定。

These are rather simple cases but normally other form controls further down the page can show/hide/disable entire sections or individual form controls. 这些是相当简单的情况,但通常页面下方的其他表单控件可以显示/隐藏/禁用整个部分或单个表单控件。 As you can see there are cases where showing/hiding each control has exceptions and overlapping cases. 如您所见,在某些情况下,显示/隐藏每个控件都有例外和重叠的情况。

在此处输入图片说明

Copying part of the AngularJS library is a distinct possibility and you could even generalise by using JQuery to select all of the elements that have a ng-show attribute and adding event listeners to add and remove classes. 复制AngularJS库的一部分是一种明显的可能性,您甚至可以通过使用JQuery选择具有ng-show属性的所有元素并添加事件侦听器以添加和删除类来进行概括。

But you have to consider whether it's worth reinventing part of a very nice, shiny wheel that will definitely give you the result you're after. 但是,您必须考虑是否值得重新打造一个非常漂亮,闪亮的轮子的一部分,该轮子一定会给您带来您所追求的结果。 Maintainability often comes from creating or using a framework, such as Angular JS (I prefer React), that can be reused time and time again before you even attempt to create something specific. 可维护性通常来自于创建或使用框架,例如Angular JS(我更喜欢React),在您尝试创建特定的东西之前,可以一次又一次地重用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM