Requires

Provides

Form.Request.js

Handles the basic functionality of submitting a form and updating a dom element with the result.

License:
MIT-style license
Authors:
Aaron Newton
  1. 29
  2. 30
  3. 31
  4. 32
  5. 33
  6. 34
  7. 35
  8. 36
  9. 37
  10. 38
  11. 39
if (!window.Form) window.Form = {}; (function(){ Form.Request = new Class({ Binds: ['onSubmit', 'onFormValidate'], Implements: [Options, Events, Class.Occlude], options: {

onFailure: function(){}, onSuccess: #function(){}, //aliased to onComplete, onSend: function(){}

  1. 43
  2. 44
  3. 45
  4. 46
  5. 47
  6. 48
  7. 49
  8. 50
  9. 51
  10. 52
  11. 53
  12. 54
  13. 55
  14. 56
  15. 57
  16. 58
  17. 59
  18. 60
  19. 61
  20. 62
  21. 63
  22. 64
  23. 65
  24. 66
  25. 67
  26. 68
  27. 69
  28. 70
  29. 71
  30. 72
  31. 73
  32. 74
  33. 75
  34. 76
  35. 77
  36. 78
  37. 79
  38. 80
  39. 81
  40. 82
  41. 83
  42. 84
  43. 85
  44. 86
  45. 87
  46. 88
  47. 89
  48. 90
  49. 91
  50. 92
  51. 93
  52. 94
  53. 95
  54. 96
  55. 97
  56. 98
  57. 99
  58. 100
  59. 101
  60. 102
  61. 103
  62. 104
  63. 105
  64. 106
  65. 107
  66. 108
  67. 109
  68. 110
requestOptions: { evalScripts: true, useSpinner: true, emulation: false, link: 'ignore' }, sendButtonClicked: true, extraData: {}, resetForm: true }, property: 'form.request', initialize: function(form, update, options){ this.element = document.id(form); if (this.occlude()) return this.occluded; this.update = document.id(update); this.setOptions(options); this.makeRequest(); if (this.options.resetForm){ this.request.addEvent('success', function(){ Function.attempt(function(){ this.element.reset(); }.bind(this)); if (window.OverText) OverText.update(); }.bind(this)); } this.attach(); }, toElement: function(){ return this.element; }, makeRequest: function(){ this.request = new Request.HTML(Object.merge({ update: this.update, emulation: false, spinnerTarget: this.element, method: this.element.get('method') || 'post' }, this.options.requestOptions)).addEvents({ success: function(tree, elements, html, javascript){ ['complete', 'success'].each(function(evt){ this.fireEvent(evt, [this.update, tree, elements, html, javascript]); }, this); }.bind(this), failure: function(){ this.fireEvent('complete', arguments).fireEvent('failure', arguments); }.bind(this), exception: function(){ this.fireEvent('failure', arguments); }.bind(this) }); }, attach: function(attach){ attach = attach != null ? attach : true; method = attach ? 'addEvent' : 'removeEvent'; this.element[method]('click:relay(button, input[type=submit])', this.saveClickedButton.bind(this)); var fv = this.element.retrieve('validator'); if (fv) fv[method]('onFormValidate', this.onFormValidate); else this.element[method]('submit', this.onSubmit); }, detach: function(){ this.attach(false); return this; },

public method

  1. 113
  2. 114
  3. 115
  4. 116
enable: function(){ this.attach(); return this; },

public method

  1. 119
  2. 120
  3. 121
  4. 122
  5. 123
  6. 124
disable: function(){ this.detach(); return this; }, onFormValidate: function(valid, form, e){

if there’s no event, then this wasn’t a submit event

  1. 126
  2. 127
  3. 128
  4. 129
  5. 130
  6. 131
  7. 132
  8. 133
  9. 134
  10. 135
  11. 136
if (!e) return; var fv = this.element.retrieve('validator'); if (valid || (fv && !fv.options.stopOnFailure)){ if (e && e.stop) e.stop(); this.send(); } }, onSubmit: function(e){ var fv = this.element.retrieve('validator'); if (fv){

form validator was created after Form.Request

  1. 138
  2. 139
  3. 140
  4. 141
  5. 142
  6. 143
  7. 144
  8. 145
  9. 146
  10. 147
  11. 148
  12. 149
  13. 150
  14. 151
  15. 152
  16. 153
  17. 154
  18. 155
  19. 156
  20. 157
  21. 158
  22. 159
  23. 160
  24. 161
  25. 162
  26. 163
  27. 164
  28. 165
  29. 166
  30. 167
  31. 168
  32. 169
  33. 170
  34. 171
  35. 172
  36. 173
  37. 174
  38. 175
  39. 176
  40. 177
  41. 178
  42. 179
  43. 180
  44. 181
  45. 182
  46. 183
  47. 184
  48. 185
  49. 186
  50. 187
  51. 188
  52. 189
  53. 190
  54. 191
  55. 192
  56. 193
  57. 194
  58. 195
  59. 196
  60. 197
  61. 198
  62. 199
  63. 200
  64. 201
  65. 202
  66. 203
  67. 204
  68. 205
  69. 206
  70. 207
  71. 208
  72. 209
  73. 210
  74. 211
this.element.removeEvent('submit', this.onSubmit); fv.addEvent('onFormValidate', this.onFormValidate); this.element.validate(); return; } if (e) e.stop(); this.send(); }, saveClickedButton: function(event, target){ if (!this.options.sendButtonClicked) return; if (!target.get('name')) return; this.options.extraData[target.get('name')] = target.get('value') || true; this.clickedCleaner = function(){ delete this.options.extraData[target.get('name')]; this.clickedCleaner = function(){}; }.bind(this); }, clickedCleaner: function(){}, send: function(){ var str = this.element.toQueryString().trim(); var data = Object.toQueryString(this.options.extraData); if (str) str += "&" + data; else str = data; this.fireEvent('send', [this.element, str.parseQueryString()]); this.request.send({data: str, url: this.element.get("action")}); this.clickedCleaner(); return this; } }); Element.Properties.formRequest = { set: function(){ var opt = Array.link(arguments, {options: Type.isObject, update: Type.isElement, updateId: Type.isString}); var update = opt.update || opt.updateId; var updater = this.retrieve('form.request'); if (update){ if (updater) updater.update = document.id(update); this.store('form.request:update', update); } if (opt.options){ if (updater) updater.setOptions(opt.options); this.store('form.request:options', opt.options); } return this; }, get: function(){ var opt = Array.link(arguments, {options: Type.isObject, update: Type.isElement, updateId: Type.isString}); var update = opt.update || opt.updateId; if (opt.options || update || !this.retrieve('form.request')){ if (opt.options || !this.retrieve('form.request:options')) this.set('form.request', opt.options); if (update) this.set('form.request', update); this.store('form.request', new Form.Request(this, this.retrieve('form.request:update'), this.retrieve('form.request:options'))); } return this.retrieve('form.request'); } }; Element.implement({ formUpdate: function(update, options){ this.get('formRequest', update, options).send(); return this; } }); })();