Permalink
Browse files

Rewrote the errors module in prototype style.

  • Loading branch information...
1 parent a5004c5 commit 3ac370abae8b34e79d59d2d6f0f7e4fbdcdd54cd @ratnikov committed Feb 26, 2009
Showing with 107 additions and 39 deletions.
  1. +48 −39 src/errors.js
  2. +34 −0 test/errors_test.html
  3. +25 −0 test/errors_test.js
View
@@ -1,44 +1,53 @@
AjaxResource.Errors = function(selector) {
- var private = { };
- jQuery(document).ready(function() {
- private.div = jQuery(jQuery.unique(jQuery(selector).find('div.error')));
- private.ul = private.div.find("ul");
+ this._error_div = jQuery(selector).find('div.error');
+};
+
+AjaxResource.Errors.prototype.error_div = function() {
+ return this._error_div;
+};
+
+AjaxResource.Errors.prototype.ul = function() {
+ return this.error_div().find("ul");
+};
+
+AjaxResource.Errors.prototype.append = function(error) {
+ var self = this;
+ jQuery.each(errors, function() {
+ if (this.length == 2) {
+ var desc = this[0];
+ var msg = this[1];
+ } else {
+ throw "Unsupported error format for: "+this;
+ }
+
+ var full_message;
+ if (desc !== 'base') {
+ full_message = desc + ' ' + msg;
+ } else {
+ // Use only the error message for base errors
+ full_message = msg;
+ }
+
+ self.ul().append("<li>" + full_message + "</li>");
});
+};
+
+AjaxResource.Errors.prototype.clear = function() {
+ this.ul().html("");
+ this.hide();
+};
+
+AjaxResource.Errors.prototype.set = function(errors) {
+ this.clear();
+ this.append(errors);
+ this.show();
+};
+
+AjaxResource.Errors.hide = function() {
+ this.error_div().hide();
+};
- this.clear = function() {
- private.ul.html("");
- this.hide();
- };
-
- this.append = function(errors) {
- jQuery.each(errors, function() {
- // assuming the errors are in the [ desc, message ] format
- if (this.length == 2) {
- var desc = this[0];
- var msg = this[1];
- } else {
- throw "Util.Errors: Unsupported error format";
- }
-
- var full_message;
- if (desc !== 'base') {
- full_message = desc+" "+msg;
- } else {
- // if it's a base error, add just the error message
- full_message = msg;
- }
-
- private.ul.append("<li>"+full_message+"</li>");
- });
- };
-
- this.set = function(errors) {
- this.clear();
- this.append(errors);
- this.show();
- };
-
- this.hide = function() { private.div.hide(); };
- this.show = function() { private.div.show(); };
+AjaxResource.Errors.show = function() {
+ this.error_div().show();
};
View
@@ -0,0 +1,34 @@
+<html>
+ <head>
+ <script src="../lib/jquery-1.3.1.js"></script>
+
+ <script src="../src/init.js"></script>
+ <script src="../src/errors.js"></script>
+
+ <script src="test_helper.js"></script>
+ <script src="../lib/testrunner.js"></script>
+ <link rel="stylesheet" href="../lib/testsuite.css" />
+ </head>
+ <body>
+ <script src="errors_test.js"></script>
+
+ <h1>AjaxResource.Errors test</h1>
+ <h2 id="banner"></h2>
+ <h2 id="userAgent"></h2>
+
+ <ol id="tests"></ol>
+
+ <div id="main">
+ <!--ul id="external-ul"></ul-->
+ <div class="error" id="main-error-div">
+ <ul id="main-ul"></ul>
+ </div>
+
+ <div id="inner-div">
+ <div class="error" id="inner-error-div">
+ <ul id="inner-ul"></ul>
+ </div>
+ </div>
+ </div>
+ </body>
+</html>
View
@@ -0,0 +1,25 @@
+
+jQuery(document).ready(function() {
+ test("Truthitest", function() {
+ ok(true);
+ });
+
+ module("Init");
+ test("Should recognize error div correctly", function() {
+ inner_errors = new AjaxResource.Errors('#inner-div');
+ same(inner_errors.error_div().get(), jQuery("#inner-error-div").get(), "Should return only the inner div");
+
+ main_errors = new AjaxResource.Errors('#main');
+ same(main_errors.error_div().get(), jQuery("#main-error-div, #inner-error-div").get(), "Should return both error divs");
+
+ all_errors = new AjaxResource.Errors(document);
+ same(all_errors.error_div().get(), jQuery("div.error").get(), "Should match all error divs on the page.");
+ });
+
+ module("#ul");
+ test("Should return ul within the error div", function() {
+ var inner_ul = (new AjaxResource.Errors('#inner-div')).ul();
+ matches_only(inner_ul, '#inner-ul', "Should include the inner ul");
+ matches_only((new AjaxResource.Errors('#main')).ul(), [ '#inner-ul', '#main-ul', ], "Should include both inner and main uls");
+ });
+});

0 comments on commit 3ac370a

Please sign in to comment.