-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptjax.js
More file actions
132 lines (126 loc) · 3.26 KB
/
scriptjax.js
File metadata and controls
132 lines (126 loc) · 3.26 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
jQuery scriptjax plugin
MIT license.
*/
(function($) {
var settings = {
'ajaxSubmitClass' : 'ajaxSubmitButton',
'ajaxSpinnerClass' : 'ajaxSpinner',
};
/*******************************
* Handler methods
*******************************/
var handlers = {
redirect: function(update){
window.location = update.location;
},
updateData: function (update) {
pageData[update.key] = update.val;
},
updateHTML: function (update) {
$(update.selector).html(update.html);
},
addClass: function (update) {
$(update.selector).addClass(update.cls);
},
deleteClass: function (update) {
$(update.selector).removeClass(update.cls);
},
updateValue: function (update) {
$(update.selector).val(update.val);
},
disableField: function (update) {
$(update.selector).disable();
},
enableFields: function (update) {
$(update.selector).enable();
}
}
var methods = {
init : function(options) {
if(options) {
$.extend(settings, options);
}
},
addAjaxSubmit : function (formid, responseObj) {
var destAction = $(formid).attr('action');
$(formid + ' input.ajaxSubmit').unbind('click');
$(formid + ' input.ajaxSubmit').click(function() {
$(formid + ' ' + settings.ajaxSpinnerClass).removeClass('hidden');
var query = $(formid).serializeArray(), data = {};
data[this.name] = this.value;
for (i in query) {
if (isdefined(data[query[i].name])) {
if (jQuery.isArray(data[query[i].name])) {
tmp = data[query[i].name];
tmp.push(query[i].value);
data[query[i].name] = tmp;
} else {
curr = [];
curr.push(data[query[i].name]);
curr.push(query[i].value);
data[query[i].name] = curr;
}
} else {
data[query[i].name] = query[i].value;
}
}
submitForm(this.id, data, destAction, formid, responseObj);
});
},
ajaxUpdate: function(updates) {
updatePage(updates)
}
};
$.fn.scriptjax = function(method) {
// Method calling logic
if(methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if( typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.scriptjax');
}
};
/*******************************
* Private methods
*******************************/
function submitForm(fields, formData, destAction, formid, responseObj) {
formData['AJAX_VALIDATE_FIELDS'] = fields;
formData['AJAX_VALIDATE'] = 1;
$.ajax( {
type : "POST",
url : destAction,
data : formData,
dataType : 'json',
success : function(returnData) {
$(formid + ' ' + settings.ajaxSpinnerClass).addClass('hidden');
updatePage(returnData)
if(typeof responseObj !== 'undefined'){
if(errors > 0){
responseObj.errors(data);
}else{
responseObj.success(data);
}
}
},
error : function(XHR, msg, error) {
$('.spin-ajax-loader-box').addClass('hidden');
if(responseObj != undefined){
responseObj.fail();
}
}
});
}
function updatePage(updates){
if (updates) {
var len=updates.length;
for(var i=0; i<len; i++) {
var update = updates[i];
if (handlers[update.type]){
handlers[update.type](update);
}
}
}
}
})(jQuery);