jQuery断言支持/防御性编程? [英] jQuery assertion support / defensive programming?

查看:95
本文介绍了jQuery断言支持/防御性编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



对于基本的断言检查,jQuery中是否有内置的支持,主要是像预期的返回元素数。例如我可能有一个简单的如下所示:

  $(#btnSignup)click(function(){
return validateForm ;
);

现在有很多理由为什么 $(#btnSignup)可能不会完全返回1个项目:




  • 您错误按钮ID名称

  • 有人重命名错误

  • 它不存在于页面上

  • 有两个元素与该ID错误

  • 您正在使用ASP.NET MVC,您不小心使用 HtmlHelper.Button(...)而不是生成按钮的HTML HtmlHelper.Submit() Button(...)方法不会为按钮元素创建ID。



现在在这种情况下(和许多实例),我的应用程序根本不起作用,除非从选择器返回一个项目。所以我总是想要被告知,如果 $(@#btnSignup)不会完全返回1个项目。那我该怎么办呢?我很好,如果这是一个例外,或者最好是一个警报框 - 所以如果我没有在调试器中运行,那么我可以告诉你。



我正在寻找对于这样的语法,这与.NET的 Single() 扩展方法。

  $(#btnSignup)。assertSingle()。click(function(){
return validateForm ;
);



$(#btnSignup)。assertSize(1).click(function(){
return validateForm();
) ;

我本来会很乐意为此代码总是运行并告诉谁在那里有一个问题。它显然不是性能问题运行这个额外的代码永远。在这种情况下,如果 #btnSignup 不存在,我的代码将被破坏。



我确信这个问题已经被殴打死亡,有许多解决办法 - 但有人可以指出我的一些吗?



我没有看到jQuery内置的内容,并且会想知道最好的插件是什么。我更希望在页面上有一些可以继续看我的东西,并告诉我,如果这是一个问题。我甚至不会反对对错误报告服务进行的AJAX调用。

解决方案

看起来不像有内置的东西但是编写扩展名并不是太难了:

  $。fn.assertSize = function(size){
if (this.length!= size){
alert(Expected+ size +elements,but got+ this.length +。
//或者什么,也许使用console.log更不引人注目
}
返回这个;
};

使用方法与您在问题中的建议完全相同。

  $(#btnSignup)。assertSize(1).click(function(){
return validateForm();
);

请注意,即使断言失败,该函数以当前形式返回成功。你所链接的任何东西仍然被执行。使用 return false; 而不是返回此; 停止进一步执行链。


Is there any built in support in jQuery for basic assertion checking, primarily of things like 'expected number of returned elements'.

For instance I may have a simple statement like this :

 $("#btnSignup").click(function() {
     return validateForm();
 );

Now theres plenty of reasons why $("#btnSignup") may not return exactly 1 item :

  • You mistyped the button id name
  • Somebody renamed it by mistake
  • It doesnt exist on the page
  • There are TWO elements with that id by mistake
  • You are using ASP.NET MVC and you accidentally generated the HTML for the button with HtmlHelper.Button(...) instead of HtmlHelper.Submit(). The Button(...) method does NOT create an ID for the button element.

Now in this instance (and many instances) my application simply won't work unless EXACTLY one item is returned from the selector. So i ALWAYS want to be told if $("@#btnSignup") doesn't return exactly 1 item. So how can I do this?! I'm fine if this is an exception or preferably an alert box - so if I'm not running in a debugger then I can be told.

I'm looking for syntax somthing like this - which is similar in functionality to .NET's Single() extension method.

 $("#btnSignup").assertSingle().click(function() {
     return validateForm();
 );

 or 

 $("#btnSignup").assertSize(1).click(function() {
     return validateForm();
 );

I'd personally be fine for this code to ALWAYS run and tell whoever is there that there is a problem. its obviously not a performance issue to be running this extra code for all eternity. In this instance my code is broken if #btnSignup doesn't exist.

I'm sure this issue has been beaten to death and there are many solutions - but can somebody point me to some of them?

I dont see anything built into jQuery and would wonder what the best plugin is. I'd much rather just have something on the page that can keep 'watching' over me and tell me if theres an issue. I wouldn't even be opposed to an AJAX call being made to an error reporting service.

解决方案

It doesn't look like there is anything built-in. But writing an extension isn't too hard:

$.fn.assertSize = function(size) { 
  if (this.length != size) { 
    alert("Expected " + size + " elements, but got " + this.length + ".");
    // or whatever, maybe use console.log to be more unobtrusive
  }
  return this;
};

Usage is exactly as you propose in your question.

$("#btnSignup").assertSize(1).click(function() {
  return validateForm();
);

Note that in its current form the function returns successfully even if the assertion fails. Anything you have chained will still be executed. Use return false; instead of return this; to stop further execution of the chain.

这篇关于jQuery断言支持/防御性编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆