-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathRestForm.coffee
67 lines (57 loc) · 1.49 KB
/
RestForm.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
57
58
59
60
61
62
63
64
65
66
67
goog.provide 'wzk.ui.form.RestForm'
goog.require 'goog.events'
goog.require 'goog.net.EventType'
goog.require 'wzk.ui.form.ErrorNotifier'
goog.require 'goog.dom.dataset'
goog.require 'wzk.ui.form.BackgroundForm'
###*
Decorating an existing form to internal structure and sends via REST API.
Validation errors are propagated from API to Html form. The form is rendered only once.
###
class wzk.ui.form.RestForm extends wzk.ui.form.BackgroundForm
###*
@constructor
@extends {wzk.ui.form.BackgroundForm}
@param {wzk.resource.Client} client
@param {wzk.dom.Dom} dom
###
constructor: (client, dom) ->
super client, dom
@notifier = null
@url = ''
###*
@override
@suppress {checkTypes}
###
decorate: (form) ->
super form
@notifier = new wzk.ui.form.ErrorNotifier @dom, form
@url = String(goog.dom.dataset.get(form, 'api') ? form.action)
###*
@param {Element} form
###
send: (form) ->
super form
data = wzk.ui.form.form2Json form
@client.request @url, form.getAttribute('method'), data, @onSuccess, @onError
###*
@override
###
onError: (json) =>
super json
@setButtonsEnabled true
@showErrors json?['errors']
###*
@override
###
onSuccess: (data) =>
super data
@setButtonsEnabled true
@notifier.hideAll()
@dispatchEvent wzk.ui.form.BackgroundForm.EventType.SAVED
###*
@protected
@param {Object.<string, string>} errors
###
showErrors: (errors) ->
@notifier.notify errors