-
Notifications
You must be signed in to change notification settings - Fork 2
/
autosave.js.coffee
56 lines (46 loc) · 1.37 KB
/
autosave.js.coffee
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
$.fn.Autosave = (options) ->
$form = this
required_fields = options and options.required_fields or []
started = false
done = false
match = /^(\/\w+\/\w+\/\w+|\/\w+)\/(\w+)$/.exec($form.attr("action"))
id = match.pop() if match
required_fields_populated = ->
if required_fields.length > 0
mode = options.require_mode or "all"
return _[mode](required_fields, (field) ->
$.trim $(field).val() or $(field).text()
)
true
save = ->
return if not started or done
if required_fields_populated()
unless id
$.post($form.attr("action"), $form.serialize(), ((data) ->
id = data._id
$form.attr "action", $form.attr("action") + "/" + id
hidden = "<input type=\"hidden\" name=\"_method\" value=\"put\" />"
$form.append hidden
), "json").error ->
console.log "validation error"
false
else
$.post($form.attr("action"), $form.serialize(), null, "json").error ->
console.log "validation error"
false
setInterval (->
save()
), 6000
$(window).unload ->
started = true
save()
auto_save =
start: ->
started = true
stop: ->
started = false
kill: ->
done = true
$form.submit ->
auto_save.kill()
auto_save