Skip to content

Commit ba89a64

Browse files
committed
Merge pull request #9 from dmglab/master
eliminated switch case in fieldDirective
2 parents 41f2436 + 2248918 commit ba89a64

File tree

1 file changed

+43
-51
lines changed

1 file changed

+43
-51
lines changed
Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,50 @@
11
'use strict';
22

3-
angularApp.directive('fieldDirective', function ($http, $compile) {
3+
// coffeescript's for in loop
4+
var __indexOf = [].indexOf || function(item) {
5+
for (var i = 0, l = this.length; i < l; i++) {
6+
if (i in this && this[i] === item) return i;
7+
}
8+
return -1;
9+
};
410

5-
var getTemplateUrl = function(field) {
6-
var type = field.field_type;
7-
var templateUrl = '';
11+
angularApp.directive('fieldDirective', function($http, $compile) {
812

9-
switch(type) {
10-
case 'textfield':
11-
templateUrl = './views/directive-templates/field/textfield.html';
12-
break;
13-
case 'email':
14-
templateUrl = './views/directive-templates/field/email.html';
15-
break;
16-
case 'textarea':
17-
templateUrl = './views/directive-templates/field/textarea.html';
18-
break;
19-
case 'checkbox':
20-
templateUrl = './views/directive-templates/field/checkbox.html';
21-
break;
22-
case 'date':
23-
templateUrl = './views/directive-templates/field/date.html';
24-
break;
25-
case 'dropdown':
26-
templateUrl = './views/directive-templates/field/dropdown.html';
27-
break;
28-
case 'hidden':
29-
templateUrl = './views/directive-templates/field/hidden.html';
30-
break;
31-
case 'password':
32-
templateUrl = './views/directive-templates/field/password.html';
33-
break;
34-
case 'radio':
35-
templateUrl = './views/directive-templates/field/radio.html';
36-
break;
37-
}
38-
return templateUrl;
39-
}
13+
var getTemplateUrl = function(field) {
14+
var type = field.field_type;
15+
var templateUrl = './views/directive-templates/field/';
16+
var supported_fields = [
17+
'textfield',
18+
'email',
19+
'textarea',
20+
'checkbox',
21+
'date',
22+
'dropdown',
23+
'hidden',
24+
'password',
25+
'radio'
26+
]
4027

41-
var linker = function(scope, element) {
42-
// GET template content from path
43-
var templateUrl = getTemplateUrl(scope.field);
44-
$http.get(templateUrl).success(function(data) {
45-
element.html(data);
46-
$compile(element.contents())(scope);
47-
});
28+
if (__indexOf.call(supported_fields, type) >= 0) {
29+
return templateUrl += type + '.html';
4830
}
31+
}
32+
33+
var linker = function(scope, element) {
34+
// GET template content from path
35+
var templateUrl = getTemplateUrl(scope.field);
36+
$http.get(templateUrl).success(function(data) {
37+
element.html(data);
38+
$compile(element.contents())(scope);
39+
});
40+
}
4941

50-
return {
51-
template: '<div>{{field}}</div>',
52-
restrict: 'E',
53-
scope: {
54-
field:'='
55-
},
56-
link: linker
57-
};
58-
});
42+
return {
43+
template: '<div>{{field}}</div>',
44+
restrict: 'E',
45+
scope: {
46+
field: '='
47+
},
48+
link: linker
49+
};
50+
});

0 commit comments

Comments
 (0)