Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Fixed multiple instatiation of forms class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandro Rodrigues committed Jul 3, 2017
1 parent e797924 commit 80360ce
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions ajax_views/static/ajax_views/forms.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@

var methods = {
submit: function (data) {
var daf = this.data('daf-data')
var daf = this.data('daf-data');
if (daf) {
daf.submit(data);
}
Expand All @@ -86,28 +86,30 @@
cacheFilesAttr: "[data-ajax-submit-cachefiles]",
canSubmitFn: null,
onRenderErrorFn: null,
handleSubmitEvent: true,
}, $.fn.djangoAjaxForms.defaults, options);

function DjangoAjaxForms($form)
{
this.$form = $form;
this.$form.data('daf-data', this);

$form.data('daf-data', this);

var self = this,
canSubmit = true;
if (opts.handleSubmitEvent) {
var self = this,
canSubmit = true;

this.$form = $form;
this.$form.on('submit', function(e) {
e.preventDefault();
this.$form.on('submit', function(e) {
e.preventDefault();

if ( $.isFunction( opts.canSubmitFn ) ) {
canSubmit = opts.canSubmitFn(self.$form);
}
if ( $.isFunction( opts.canSubmitFn ) ) {
canSubmit = opts.canSubmitFn(self.$form);
}

if (self.$form.length > 0 && canSubmit) {
self.submit();
}
});
if (self.$form.length > 0 && canSubmit) {
self.submit();
}
});
}

if (this.$form.filter(opts.cacheFilesAttr).length) {
this.cachedFiles = new ManageCacheFile(this.$form);
Expand All @@ -122,7 +124,7 @@
data: data,
method: 'POST',
dataType: 'json'
}
};

if ( !isCustomData ) {
options.contentType = false;
Expand Down Expand Up @@ -200,6 +202,7 @@
nonfielderror = true;
} else {
var $field = $form.find("#" + opts.fieldIdSelector + fieldName);

var onChange = function () {
$field.removeClass('error', 200).find('.errorlist').fadeOut(200, function () {
$(this).remove();
Expand Down Expand Up @@ -246,7 +249,12 @@
} else if ( typeof options === 'object' || ! options ) {
return this.each(function()
{
new DjangoAjaxForms($(this));
var $this = $(this);
var daf = $this.data('daf-data');

if (!daf) {
new DjangoAjaxForms($this);
}
});
} else {
$.error( 'Method ' + options + ' does not exist.' );
Expand Down

0 comments on commit 80360ce

Please sign in to comment.