-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.fugue.js
101 lines (84 loc) · 2.55 KB
/
jquery.fugue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* jQuery Fugue Plugin
* For Ajax Form submit
* Copyright (c) 2012 Lancee (xrhy.me)
* Dual licensed under the MIT and GPL licenses:
*/
!(function() {
(function($, Export) {
"use strict";
$.fugue = function(ajaxform, options) {
if (!ajaxform || ajaxform.nodeName.toLowerCase()!=="form") {
throw new Error('this is not a form');
}
$.support.formdata = Export.FormData !== undefined;
var Fugue = function() {
this.init();
};
Fugue.prototype = {
constructor: Fugue,
init: function() {
var $form = $(ajaxform).data('fugue', this),
self = this;
self.$el = $form.addClass('fugue');
options = $.extend({}, $.fugue.default, options);
options.type = $form.attr('method') || options.type;
options.url = $form.attr('action') || options.url;
$form.on('submit', function(e) {
(e.preventDefault)?e.preventDefault():e.returnValue = false;
self.submit(e);
});
self.options = options;
},
serialize: function() {
options.beforeSerialize();
if ($.support.formdata) {
var dataArray = this.$el.serializeArray(),
data = {};
$.each(dataArray, function(i, field) {
data[field.name] = field.value;
});
options.data = $.extend({}, options.data, data);
}
this.options = options;
},
submit: function(e) {
this.serialize();
options.beforeSubmit();
delete this.options['beforeSubmit'],
delete this.options['beforeSerialize'];
$.ajax(this.options);
},
options: $.fugue.default
}
return new Fugue();
}
$.fugue.default = {
beforeSerialize: function() {},
beforeSubmit: function() {},
type: 'POST',
dataType: 'json'
};
$.fn.fugue = function(options, callback) {
var fugue = $(this).data('fugue');
if ($.isFunction(options)) {
callback = options;
options = null;
}
if((typeof(options)).match('object|undefined')) {
return this.each(function(i) {
if(!fugue) {
fugue = $.fugue(this, options);
if(callback)
callback.call(fugue);
} else {
if(callback)
callback.call(fugue);
}
});
} else {
throw new Error('arguments[0] is not a instance of Object');
}
}
})(jQuery, window);
}).call(this);