-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.formobserve.js
62 lines (55 loc) · 1.72 KB
/
jquery.formobserve.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
/**
* jQuery plugin for observing forms on changes
*
* @author Nikolay Zmachinsky
*/
(function($){
var options = {
'dataAttr': 'form-observe-state',
'trackNodesSelector': 'form, .form-observe'
};
function findFormNode(node){
var node = $(node);
return node.is(options.trackNodesSelector) ? node : node.find(options.trackNodesSelector);
}
$.fn.extend({
/**
* saves the current state of the form
*/
formObserve: function(){
var forms = findFormNode(this);
forms.each(function(){
var form = $(this);
form.data(options.dataAttr, form.find(':input').serialize());
});
return this;
},
/**
* set ignore form changes flag
*/
formIgnoreChanges: function(ignore){
var forms = findFormNode(this);
forms.each(function(){
var form = $(this);
form.data(options.ignoreAttr, ignore | 0);
});
return this;
},
/**
* check if the form was updated since formObserve was called
*/
formChanged: function(){
var changed = false;
var forms = findFormNode(this);
forms.each(function(){
var form = $(this);
var storedData = form.data(options.dataAttr);
if (storedData !== undefined && form.find(':input').serialize() != storedData){
changed = true;
return false;
}
});
return changed;
}
});
})(jQuery);