Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moves event delegation to namespaced object #413

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 109 additions & 67 deletions src/rails.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,16 +326,8 @@
}
};

if (rails.fire($document, 'rails:attachBindings')) {

$.ajaxPrefilter(function(options, originalOptions, xhr){ if ( !options.crossDomain ) { rails.CSRFProtection(xhr); }});

// This event works the same as the load event, except that it fires every
// time the page is loaded.
//
// See https://github.com/rails/jquery-ujs/issues/357
// See https://developer.mozilla.org/en-US/docs/Using_Firefox_1.5_caching
$(window).on("pageshow.rails", function () {
$.rails.handlers = {
pageshow: function(){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after the )

$($.rails.enableSelector).each(function () {
var element = $(this);

Expand All @@ -351,18 +343,65 @@
$.rails.enableElement(element);
}
});
});
},

$document.delegate(rails.linkDisableSelector, 'ajax:complete', function() {
rails.enableElement($(this));
});
submit: function(e){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after the )

var form = $(e.currentTarget),
remote = form.data('remote') !== undefined,
blankRequiredInputs,
nonBlankFileInputs;

$document.delegate(rails.buttonDisableSelector, 'ajax:complete', function() {
rails.enableFormElement($(this));
});
if (!rails.allowAction(form)) return rails.stopEverything(e);

$document.delegate(rails.linkClickSelector, 'click.rails', function(e) {
var link = $(this), method = link.data('method'), data = link.data('params'), metaClick = e.metaKey || e.ctrlKey;
// skip other logic when required values are missing or file upload is present
if (form.attr('novalidate') == undefined) {
blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector);
if (blankRequiredInputs && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) {
return rails.stopEverything(e);
}
}

if (remote) {
nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector);
if (nonBlankFileInputs) {
// slight timeout so that the submit button gets properly serialized
// (make it easy for event handler to serialize form without disabled values)
setTimeout(function(){ rails.disableFormElements(form); }, 13);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after the ) of function

var aborted = rails.fire(form, 'ajax:aborted:file', [nonBlankFileInputs]);

// re-enable form elements if event bindings return false (canceling normal form submission)
if (!aborted) { setTimeout(function(){ rails.enableFormElements(form); }, 13); }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after the ) of function


return aborted;
}

rails.handleRemote(form);
return false;

} else {
// slight timeout so that the submit button gets properly serialized
setTimeout(function(){ rails.disableFormElements(form); }, 13);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after the ) of function

}
},

ajaxCompleteLink: function($el){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after the )

rails.enableElement($el);
},

ajaxCompleteButton: function($el){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after the )

rails.enableFormElement($el);
},

ajaxCompleteForm: function(e, ctx){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after the )

if (ctx == e.target) rails.enableFormElements($(ctx));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we didn't had this if guard before - what's the rationale behind it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure about the logic behind this conditional, but instead of ripping it out I left it in. I'm assuming it has a reason for being there.

},

ajaxSendForm: function(e, ctx){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after the )

if (ctx == e.target) rails.disableFormElements($(ctx));
},

clickLink: function(e){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after the )

var link = $(e.currentTarget), method = link.data('method'), data = link.data('params'), metaClick = e.metaKey || e.ctrlKey;
if (!rails.allowAction(link)) return rails.stopEverything(e);

if (!metaClick && link.is(rails.linkDisableSelector)) rails.disableElement(link);
Expand All @@ -383,10 +422,10 @@
rails.handleMethod(link);
return false;
}
});
},

$document.delegate(rails.buttonClickSelector, 'click.rails', function(e) {
var button = $(this);
clickButton: function(e){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after the )

var button = $(e.currentTarget);

if (!rails.allowAction(button)) return rails.stopEverything(e);

Expand All @@ -400,73 +439,76 @@
handleRemote.fail( function() { rails.enableFormElement(button); } );
}
return false;
});
},

$document.delegate(rails.inputChangeSelector, 'change.rails', function(e) {
var link = $(this);
clickInput: function(e){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after the )

var button = $(e.currentTarget);

if (!rails.allowAction(button)) return rails.stopEverything(e);

// register the pressed submit button
var name = button.attr('name'),
data = name ? {name:name, value:button.val()} : null;

button.closest('form').data('ujs:submit-button', data);
},

change: function(e){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after the )

var link = $(e.currentTarget);
if (!rails.allowAction(link)) return rails.stopEverything(e);

rails.handleRemote(link);
return false;
});

$document.delegate(rails.formSubmitSelector, 'submit.rails', function(e) {
var form = $(this),
remote = form.data('remote') !== undefined,
blankRequiredInputs,
nonBlankFileInputs;

if (!rails.allowAction(form)) return rails.stopEverything(e);
}
}

// skip other logic when required values are missing or file upload is present
if (form.attr('novalidate') == undefined) {
blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector);
if (blankRequiredInputs && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) {
return rails.stopEverything(e);
}
}
if (rails.fire($document, 'rails:attachBindings')) {

if (remote) {
nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector);
if (nonBlankFileInputs) {
// slight timeout so that the submit button gets properly serialized
// (make it easy for event handler to serialize form without disabled values)
setTimeout(function(){ rails.disableFormElements(form); }, 13);
var aborted = rails.fire(form, 'ajax:aborted:file', [nonBlankFileInputs]);
$.ajaxPrefilter(function(options, originalOptions, xhr){ if ( !options.crossDomain ) { rails.CSRFProtection(xhr); }});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after the ) of function


// re-enable form elements if event bindings return false (canceling normal form submission)
if (!aborted) { setTimeout(function(){ rails.enableFormElements(form); }, 13); }
// This event works the same as the load event, except that it fires every
// time the page is loaded.
//
// See https://github.com/rails/jquery-ujs/issues/357
// See https://developer.mozilla.org/en-US/docs/Using_Firefox_1.5_caching
$(window).on("pageshow.rails", function(){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after the )

$.rails.handlers.pageshow();
});

return aborted;
}
$document.delegate(rails.linkDisableSelector, 'ajax:complete', function() {
$.rails.handlers.ajaxCompleteLink($(this));
});

rails.handleRemote(form);
return false;
$document.delegate(rails.buttonDisableSelector, 'ajax:complete', function() {
$.rails.handlers.ajaxCompleteButton($(this));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe is better to only pass this instead of a jQuery object to keep the API consistent.

});

} else {
// slight timeout so that the submit button gets properly serialized
setTimeout(function(){ rails.disableFormElements(form); }, 13);
}
$document.delegate(rails.linkClickSelector, 'click.rails', function(e) {
return $.rails.handlers.clickLink(e);
});

$document.delegate(rails.formInputClickSelector, 'click.rails', function(event) {
var button = $(this);
$document.delegate(rails.buttonClickSelector, 'click.rails', function(e) {
return $.rails.handlers.clickButton(e);
});

if (!rails.allowAction(button)) return rails.stopEverything(event);
$document.delegate(rails.inputChangeSelector, 'change.rails', function(e) {
return $.rails.handlers.change(e);
});

// register the pressed submit button
var name = button.attr('name'),
data = name ? {name:name, value:button.val()} : null;
$document.delegate(rails.formSubmitSelector, 'submit.rails', function(e){
return $.rails.handlers.submit(e);
});

button.closest('form').data('ujs:submit-button', data);
$document.delegate(rails.formInputClickSelector, 'click.rails', function(event) {
$.rails.handlers.clickInput(event);
});

$document.delegate(rails.formSubmitSelector, 'ajax:send.rails', function(event) {
if (this == event.target) rails.disableFormElements($(this));
$.rails.handlers.ajaxSendForm(event, this);
});

$document.delegate(rails.formSubmitSelector, 'ajax:complete.rails', function(event) {
if (this == event.target) rails.enableFormElements($(this));
$.rails.handlers.ajaxCompleteForm(event, this);
});

$(function(){
Expand Down