The code is now completely covered in specs
[lyrix.git] / public / javascripts / lowpro / remote.js
blob7f9dab41c419fdf76bba32c1a094872b2e6cc5c0
1 Form.Methods.serialize = function(form, getHash, buttonPressed) {
2 var elements = Form.getElements(form).reject(function(element) {
3 return ['submit', 'button', 'image'].include(element.type);
4 });
6 if (buttonPressed || (buttonPressed = form.getElementsBySelector('*[type=submit]').first()))
7 elements.push(buttonPressed);
9 return Form.serializeElements(elements, getHash);
12 Element.addMethods();
14 Remote = {
15 Base: {
16 initialize : function(options) {
17 this.options = Object.extend({
18 evaluateScripts : true
19 }, options || {});
21 _makeRequest : function(options) {
22 if (options.update) new Ajax.Updater(options.update, options.url, options);
23 else new Ajax.Request(options.url, options);
24 return false;
29 Remote.Link = Behavior.create({
30 onclick : function() {
31 var options = Object.extend({ url : this.element.href, method : 'get' }, this.options);
32 return this._makeRequest(options);
34 });
36 Object.extend(Remote.Link.prototype, Remote.Base);
38 Remote.Form = Behavior.create({
39 onclick : function(e) {
40 var sourceElement = Event.element(e);
42 if (sourceElement.nodeName.toLowerCase() == 'input' &&
43 sourceElement.type == 'submit')
44 this._submitButton = sourceElement;
46 onsubmit : function() {
47 var options = Object.extend({
48 url : this.element.action,
49 method : this.element.method,
50 parameters : Form.serialize(this.element, false, this._submitButton)
51 }, this.options);
52 this._submitButton = null;
53 return this._makeRequest(options);
55 });
57 Object.extend(Remote.Form.prototype, Remote.Base);