Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // We use a closure to prevent polluting the global namespace.
- var AjaxifyForms = (function($, _) {
- "use strict";
- // Use strict makes it harder to make stupid mistakes.
- var AjaxifyForms = {
- $form: "",
- options: {
- formSelector: "form",
- successFunction: null
- },
- init: function(options) {
- // Initialize
- var self = this;
- this.options = _.extend(this.options, options);
- this.$form = $(options.formSelector);
- // Bind
- this.$form.on("submit", function(e) {
- self.interceptSubmit.call(self, e, this);
- });
- },
- interceptSubmit: function(e, form) {
- // Will interecept any form submissions and turn them into ajax.
- e.preventDefault();
- var $form = $(form);
- $.ajax(
- $form.attr("action"),
- {
- type: $form.attr("method"),
- data: $form.serialize(),
- success: this.options.successFunction
- }
- );
- }
- };
- return AjaxifyForms;
- }($, _));
- // Ajaxify forms is now much more modular and can be reused.
- var ajaxifyForms = AjaxifyForms.init({
- formSelector: ".some-special-form"
- });
Add Comment
Please, Sign In to add comment