Skip to content

Instantly share code, notes, and snippets.

@andrevinsky
Forked from ddemid/forms.js
Created May 19, 2014 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrevinsky/4dbf321db50a25ab195f to your computer and use it in GitHub Desktop.
Save andrevinsky/4dbf321db50a25ab195f to your computer and use it in GitHub Desktop.
app.directive('remoteForm', function($http){
function link(scope, formElement, attrs, formController) {
scope.input = {};
scope.submit = function (){
$http.post(attrs.remoteForm, scope.input)
.success(function(data, status, headers, config) {
// $window.location.reload();
}).
error(function(data, status, headers, config) {
angular.forEach(data, function (value, key){
if (key == 'non_field_errors') return;
if (formController[key])
formController[key].$setValidity('remote', false);
})
});
};
formElement.submit(scope.submit);
}
return {
require: 'form',
scope: {
},
link: link,
compile: function (temaplateElement, templateAttrs) {
return {
pre: function (scope, element, attrs){
// element.find('[remote-form-control]').each(function (){
// var name = $(this).attr('remote-form-control');
// $(this).attr('ng-model', 'input.' + name);
// });
},
post: link
}
}
};
});
app.directive('remoteFormControl', function(){
return {
require: '^remoteForm',
compile: function (temaplateElement, templateAttrs) {
return {
pre: function (scope, element, attrs){
if (element.attr('remote-form-control')){
var name = element.attr('remote-form-control');
$(element).attr('ng-model', 'input.' + name);
input.after('<span class="help-block" ng-if="errors.' + name + '">{{ errors.' + name + ' }}</span>');
$(element).closest('.form-group').attr('ng-class', '{"has-error": form.'+name+'.$invalid}');
}
},
post: function(scope, element, attrs) {
}
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment