Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit e0ad7df

Browse files
author
Misko Hevery
committed
seperatio validation and exception handling
1 parent a8aa5af commit e0ad7df

File tree

9 files changed

+36
-37
lines changed

9 files changed

+36
-37
lines changed

angular-debug.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ var consoleNode,
3434
PRIORITY_WATCH = -1000,
3535
PRIORITY_LAST = 99999,
3636
NOOP = 'noop',
37-
NG_ERROR = 'ng-error',
3837
NG_EXCEPTION = 'ng-exception',
3938
NG_VALIDATION_ERROR = 'ng-validation-error',
4039
jQuery = window['jQuery'] || window['$'], // weirdness to make IE happy
@@ -283,10 +282,10 @@ function elementError(element, type, error) {
283282
}
284283
if (error) {
285284
element.addClass(type);
286-
element.attr(NG_ERROR, error);
285+
element.attr(type, error);
287286
} else {
288287
element.removeClass(type);
289-
element.removeAttr(NG_ERROR);
288+
element.removeAttr(type);
290289
}
291290
}
292291

@@ -1222,16 +1221,7 @@ Parser.prototype = {
12221221
for ( var i = 0; i < argsFn.length; i++) {
12231222
args.push(argsFn[i](self));
12241223
}
1225-
var pipeThis = function(){
1226-
var _this = this;
1227-
foreach(self, function(v, k) {
1228-
if (k.charAt(0) == '$') {
1229-
_this[k] = v;
1230-
}
1231-
});
1232-
};
1233-
pipeThis.prototype = self.self;
1234-
return fn.apply(new pipeThis(), args);
1224+
return fn.apply(self.state, args);
12351225
};
12361226
return function(){
12371227
return fnInvoke;
@@ -2883,12 +2873,17 @@ angularDirective("ng-bind-template", function(expression){
28832873
};
28842874
});
28852875

2876+
var REMOVE_ATTRIBUTES = {
2877+
'disabled':true,
2878+
'readonly':true,
2879+
'checked':true
2880+
};
28862881
angularDirective("ng-bind-attr", function(expression){
28872882
return function(element){
28882883
this.$onEval(function(){
28892884
foreach(this.$eval(expression), function(bindExp, key) {
28902885
var value = compileBindTemplate(bindExp).call(this, element);
2891-
if (key == 'disabled' && !toBoolean(value)) {
2886+
if (REMOVE_ATTRIBUTES[lowercase(key)] && !toBoolean(value)) {
28922887
element.removeAttr('disabled');
28932888
} else {
28942889
element.attr(key, value);
@@ -3129,7 +3124,7 @@ function valueAccessor(scope, element) {
31293124
required = required || required === '';
31303125
if (!validator) throw "Validator named '" + validatorName + "' not found.";
31313126
function validate(value) {
3132-
var error = required && !trim(value) ? "Required" : validator({self:scope, scope:{get:scope.$get, set:scope.$set}}, value);
3127+
var error = required && !trim(value) ? "Required" : validator({state:scope, scope:{get:scope.$get, set:scope.$set}}, value);
31333128
if (error !== lastError) {
31343129
elementError(element, NG_VALIDATION_ERROR, error);
31353130
lastError = error;
@@ -3402,7 +3397,7 @@ angularService("$location", function(browser){
34023397
angularService("$hover", function(browser) {
34033398
var tooltip, self = this, error, width = 300, arrowWidth = 10;
34043399
browser.hover(function(element, show){
3405-
if (show && (error = element.attr('ng-error'))) {
3400+
if (show && (error = element.attr(NG_EXCEPTION) || element.attr(NG_VALIDATION_ERROR))) {
34063401
if (!tooltip) {
34073402
tooltip = {
34083403
callout: jqLite('<div id="ng-callout"></div>'),

src/Angular.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ var consoleNode,
1010
PRIORITY_WATCH = -1000,
1111
PRIORITY_LAST = 99999,
1212
NOOP = 'noop',
13-
NG_ERROR = 'ng-error',
1413
NG_EXCEPTION = 'ng-exception',
1514
NG_VALIDATION_ERROR = 'ng-validation-error',
1615
jQuery = window['jQuery'] || window['$'], // weirdness to make IE happy
@@ -259,10 +258,10 @@ function elementError(element, type, error) {
259258
}
260259
if (error) {
261260
element.addClass(type);
262-
element.attr(NG_ERROR, error);
261+
element.attr(type, error);
263262
} else {
264263
element.removeClass(type);
265-
element.removeAttr(NG_ERROR);
264+
element.removeAttr(type);
266265
}
267266
}
268267

src/directives.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,17 @@ angularDirective("ng-bind-template", function(expression){
8080
};
8181
});
8282

83+
var REMOVE_ATTRIBUTES = {
84+
'disabled':true,
85+
'readonly':true,
86+
'checked':true
87+
};
8388
angularDirective("ng-bind-attr", function(expression){
8489
return function(element){
8590
this.$onEval(function(){
8691
foreach(this.$eval(expression), function(bindExp, key) {
8792
var value = compileBindTemplate(bindExp).call(this, element);
88-
if (key == 'disabled' && !toBoolean(value)) {
93+
if (REMOVE_ATTRIBUTES[lowercase(key)] && !toBoolean(value)) {
8994
element.removeAttr('disabled');
9095
} else {
9196
element.attr(key, value);

src/services.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ angularService("$location", function(browser){
4747
angularService("$hover", function(browser) {
4848
var tooltip, self = this, error, width = 300, arrowWidth = 10;
4949
browser.hover(function(element, show){
50-
if (show && (error = element.attr('ng-error'))) {
50+
if (show && (error = element.attr(NG_EXCEPTION) || element.attr(NG_VALIDATION_ERROR))) {
5151
if (!tooltip) {
5252
tooltip = {
5353
callout: jqLite('<div id="ng-callout"></div>'),

test/BinderTest.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -299,20 +299,20 @@ BinderTest.prototype.testIfTextBindingThrowsErrorDecorateTheSpan = function(){
299299
var span = childNode(doc, 0);
300300
assertTrue(span.hasClass('ng-exception'));
301301
assertEquals('ErrorMsg1', fromJson(span.text()));
302-
assertEquals('"ErrorMsg1"', span.attr('ng-error'));
302+
assertEquals('"ErrorMsg1"', span.attr('ng-exception'));
303303

304304
a.scope.$set('error.throw', function(){throw "MyError";});
305305
a.scope.$eval();
306306
span = childNode(doc, 0);
307307
assertTrue(span.hasClass('ng-exception'));
308308
assertTrue(span.text(), span.text().match('MyError') !== null);
309-
assertEquals('"MyError"', span.attr('ng-error'));
309+
assertEquals('"MyError"', span.attr('ng-exception'));
310310

311311
a.scope.$set('error.throw', function(){return "ok";});
312312
a.scope.$eval();
313313
assertFalse(span.hasClass('ng-exception'));
314314
assertEquals('ok', span.text());
315-
assertEquals(null, span.attr('ng-error'));
315+
assertEquals(null, span.attr('ng-exception'));
316316
};
317317

318318
BinderTest.prototype.testIfAttrBindingThrowsErrorDecorateTheAttribute = function(){
@@ -322,14 +322,14 @@ BinderTest.prototype.testIfAttrBindingThrowsErrorDecorateTheAttribute = function
322322
a.scope.$set('error.throw', function(){throw "ErrorMsg";});
323323
a.scope.$eval();
324324
assertTrue('ng-exception', doc.hasClass('ng-exception'));
325-
assertEquals('"ErrorMsg"', doc.attr('ng-error'));
325+
assertEquals('"ErrorMsg"', doc.attr('ng-exception'));
326326
assertEquals('before "ErrorMsg" after', doc.attr('attr'));
327327

328328
a.scope.$set('error.throw', function(){ return 'X';});
329329
a.scope.$eval();
330330
assertFalse('!ng-exception', doc.hasClass('ng-exception'));
331331
assertEquals('before X after', doc.attr('attr'));
332-
assertEquals(null, doc.attr('ng-error'));
332+
assertEquals(null, doc.attr('ng-exception'));
333333

334334
};
335335

@@ -474,7 +474,7 @@ BinderTest.prototype.testActionOnAHrefThrowsError = function(){
474474
};
475475
var input = c.node;
476476
input.click();
477-
assertEquals({a:"abc", b:2}, fromJson(input.attr('ng-error')));
477+
assertEquals({a:"abc", b:2}, fromJson(input.attr('ng-exception')));
478478
assertTrue("should have an error class", input.hasClass('ng-exception'));
479479

480480
// TODO: I think that exception should never get cleared so this portion of test makes no sense

test/ScopeSpec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ describe('scope/model', function(){
7878
var element = jqLite('<div></div>');
7979
var scope = createScope();
8080
scope.$tryEval('throw "myError"', element);
81-
expect(element.attr('ng-error')).toEqual('"myError"'); // errors are jsonified
81+
expect(element.attr('ng-exception')).toEqual('"myError"'); // errors are jsonified
8282
expect(element.hasClass('ng-exception')).toBeTruthy();
8383
});
8484

test/ValidatorsTest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ describe('Validator:asynchronous', function(){
118118
expect(input.hasClass('ng-input-indicator-wait')).toBeTruthy();
119119
fn("myError");
120120
expect(input.hasClass('ng-input-indicator-wait')).toBeFalsy();
121-
expect(input.attr('ng-error')).toEqual("myError");
121+
expect(input.attr('ng-validation-error')).toEqual("myError");
122122
scope.$element.remove();
123123
});
124124

test/directivesSpec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe("directives", function(){
8888
it('should error on wrong parsing of ng-repeat', function(){
8989
var scope = compile('<ul><li ng-repeat="i dont parse"></li></ul>');
9090
var log = "";
91-
log += element.attr('ng-error') + ';';
91+
log += element.attr('ng-exception') + ';';
9292
log += element.hasClass('ng-exception') + ';';
9393
expect(log).toEqual("\"Expected ng-repeat in form of 'item in collection' but got 'i dont parse'.\";true;");
9494
});

test/widgetsSpec.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,36 +54,36 @@ describe("input widget", function(){
5454
it("should process ng-validation", function(){
5555
compile('<input type="text" name="price" value="abc" ng-validate="number"/>');
5656
expect(element.hasClass('ng-validation-error')).toBeTruthy();
57-
expect(element.attr('ng-error')).toEqual('Not a number');
57+
expect(element.attr('ng-validation-error')).toEqual('Not a number');
5858

5959
scope.$set('price', '123');
6060
scope.$eval();
6161
expect(element.hasClass('ng-validation-error')).toBeFalsy();
62-
expect(element.attr('ng-error')).toBeFalsy();
62+
expect(element.attr('ng-validation-error')).toBeFalsy();
6363

6464
element.val('x');
6565
element.trigger('keyup');
6666
expect(element.hasClass('ng-validation-error')).toBeTruthy();
67-
expect(element.attr('ng-error')).toEqual('Not a number');
67+
expect(element.attr('ng-validation-error')).toEqual('Not a number');
6868
});
6969

7070
it("should process ng-required", function(){
7171
compile('<input type="text" name="price" ng-required/>');
7272
expect(element.hasClass('ng-validation-error')).toBeTruthy();
73-
expect(element.attr('ng-error')).toEqual('Required');
73+
expect(element.attr('ng-validation-error')).toEqual('Required');
7474

7575
scope.$set('price', 'xxx');
7676
scope.$eval();
7777
expect(element.hasClass('ng-validation-error')).toBeFalsy();
78-
expect(element.attr('ng-error')).toBeFalsy();
78+
expect(element.attr('ng-validation-error')).toBeFalsy();
7979

8080
element.val('');
8181
element.trigger('keyup');
8282
expect(element.hasClass('ng-validation-error')).toBeTruthy();
83-
expect(element.attr('ng-error')).toEqual('Required');
83+
expect(element.attr('ng-validation-error')).toEqual('Required');
8484
});
8585

86-
it("should process ng-required", function() {
86+
it("should process ng-required2", function() {
8787
compile('<textarea name="name">Misko</textarea>');
8888
expect(scope.$get('name')).toEqual("Misko");
8989

0 commit comments

Comments
 (0)