Created
April 22, 2014 22:20
-
-
Save wildermuthn/11196401 to your computer and use it in GitHub Desktop.
for morgan formItem directive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
angular.module('crossroadsDonateApp') | |
.directive('formItem', ['$compile', '$http', '$templateCache', function($compile, $http, $templateCache, $rootScope) { | |
var rootScope = $rootScope; | |
var getTemplate = function(contentType) { | |
var templateLoader, | |
baseUrl = 'views/', | |
templateMap = { | |
text: 'form-item-text.html', | |
radios: 'form-item-radios.html', | |
select: 'form-item-select.html', | |
}; | |
var templateUrl = baseUrl + templateMap[contentType]; | |
templateLoader = $http.get(templateUrl, {cache: $templateCache}); | |
return templateLoader; | |
}; | |
return { | |
restrict: 'A', | |
scope: { | |
item: '=', | |
change: '&', | |
}, | |
link: function postLink(scope, element, attrs) { | |
scope.valid = ""; | |
scope.$on('form.validate', function() { | |
if (scope.item.required != false && (scope.item.inputValue == '' || scope.item.inputValue == undefined)) { | |
scope.valid = "has-error"; | |
} | |
else { | |
scope.valid = ""; | |
} | |
}); | |
scope.$watch('item.inputValue', function(newValue) { | |
scope.change({}); | |
}); | |
var loader = getTemplate(scope.item.type); | |
var promise = loader.success(function(html) { | |
element.html(html); | |
}).then(function (response) { | |
scope.selected = {}; | |
if (scope.item.type == 'radios' || scope.item.type == 'select') { | |
for (var i in scope.item.values) { | |
var value = scope.item.values[i]; | |
if (value == scope.item.placeholder) { | |
scope.selected[value] = true; | |
scope.item.inputValue = value; | |
} | |
else { | |
scope.selected[value] = false; | |
} | |
} | |
} | |
$compile(element.contents())(scope); | |
}); | |
} | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment