Skip to content
This repository
Browse code

Gave external api to internal rails.js functions to make them available

to be called and modified from outside of rails.js via $.rails object.
Closes #98.
  • Loading branch information...
commit d59144177d86790891fdb99b0e3437312e04fda2 1 parent d2abd6f
Steve Schwartz authored April 18, 2011 neerajdotname committed April 18, 2011

Showing 1 changed file with 48 additions and 42 deletions. Show diff stats Hide diff stats

  1. 90  src/rails.js
90  src/rails.js
@@ -36,30 +36,29 @@
36 36
  */
37 37
 
38 38
 (function($) {
  39
+  // Shorthand to make it a little easier to call public rails functions from within rails.js
  40
+  var rails;
  41
+
  42
+  $.rails = rails = {
39 43
 	// Make sure that every Ajax request sends the CSRF token
40  
-	function CSRFProtection(xhr) {
  44
+	CSRFProtection: function(xhr) {
41 45
 		var token = $('meta[name="csrf-token"]').attr('content');
42 46
 		if (token) xhr.setRequestHeader('X-CSRF-Token', token);
43  
-	}
44  
-	if ('ajaxPrefilter' in $) {
45  
-		$.ajaxPrefilter(function(options, originalOptions, xhr){ CSRFProtection(xhr); });
46  
-	} else {
47  
-		$(document).ajaxSend(function(e, xhr){ CSRFProtection(xhr); });
48  
-	}
  47
+	},
49 48
 
50 49
 	// Triggers an event on an element and returns the event result
51  
-	function fire(obj, name, data) {
  50
+	fire: function(obj, name, data) {
52 51
 		var event = $.Event(name);
53 52
 		obj.trigger(event, data);
54 53
 		return event.result !== false;
55  
-	}
  54
+	},
56 55
 
57 56
 	// Submits "remote" forms and links with ajax
58  
-	function handleRemote(element) {
  57
+	handleRemote: function(element) {
59 58
 		var method, url, data,
60 59
 			dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType);
61 60
 
62  
-	if (fire(element, 'ajax:before')) {
  61
+	if (rails.fire(element, 'ajax:before')) {
63 62
 		if (element.is('form')) {
64 63
 			method = element.attr('method');
65 64
 			url = element.attr('action');
@@ -82,7 +81,7 @@
82 81
 					if (settings.dataType === undefined) {
83 82
 						xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
84 83
 					}
85  
-					return fire(element, 'ajax:beforeSend', [xhr, settings]);
  84
+					return rails.fire(element, 'ajax:beforeSend', [xhr, settings]);
86 85
 				},
87 86
 				success: function(data, status, xhr) {
88 87
 					element.trigger('ajax:success', [data, status, xhr]);
@@ -95,11 +94,11 @@
95 94
 				}
96 95
 			});
97 96
 		}
98  
-	}
  97
+	},
99 98
 
100 99
 	// Handles "data-method" on links such as:
101 100
 	// <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
102  
-	function handleMethod(link) {
  101
+	handleMethod: function(link) {
103 102
 		var href = link.attr('href'),
104 103
 			method = link.data('method'),
105 104
 			csrf_token = $('meta[name=csrf-token]').attr('content'),
@@ -113,54 +112,54 @@
113 112
 
114 113
 		form.hide().append(metadata_input).appendTo('body');
115 114
 		form.submit();
116  
-	}
  115
+	},
117 116
 
118  
-	function disableFormElements(form) {
  117
+	disableFormElements: function(form) {
119 118
 		form.find('input[data-disable-with], button[data-disable-with]').each(function() {
120 119
 			var element = $(this), method = element.is('button') ? 'html' : 'val';
121 120
 			element.data('ujs:enable-with', element[method]());
122 121
 			element[method](element.data('disable-with'));
123 122
 			element.attr('disabled', 'disabled');
124 123
 		});
125  
-	}
  124
+	},
126 125
 
127  
-	function enableFormElements(form) {
  126
+	enableFormElements: function(form) {
128 127
 		form.find('input[data-disable-with]:disabled, button[data-disable-with]:disabled').each(function() {
129 128
 			var element = $(this), method = element.is('button') ? 'html' : 'val';
130 129
 			if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
131 130
 			element.removeAttr('disabled');
132 131
 		});
133  
-	}
  132
+	},
134 133
 
135  
-	function allowAction(element) {
  134
+	allowAction: function(element) {
136 135
 		var message = element.data('confirm');
137  
-		return !message || (fire(element, 'confirm') && confirm(message));
138  
-	}
  136
+		return !message || (rails.fire(element, 'confirm') && confirm(message));
  137
+	},
139 138
 
140  
-	function blankInputs(form, specifiedSelector) {
  139
+	blankInputs: function(form, specifiedSelector) {
141 140
 		var blankExists = false,
142 141
 				selector = specifiedSelector || 'input';
143 142
 		form.find(selector).each(function() {
144 143
 			if (!$(this).val()) blankExists = true;
145 144
 		});
146 145
 		return blankExists;
147  
-	}
  146
+	},
148 147
 
149  
-	function nonBlankInputs(form, specifiedSelector) {
  148
+	nonBlankInputs: function(form, specifiedSelector) {
150 149
 		var nonBlankExists = false,
151 150
 				selector = specifiedSelector || 'input';
152 151
 		form.find(selector).each(function() {
153 152
 			if ($(this).val()) nonBlankExists = true;
154 153
 		});
155 154
 		return nonBlankExists;
156  
-	}
  155
+	},
157 156
 
158  
-  function stopEverything(e) {
  157
+  stopEverything: function(e) {
159 158
     e.stopImmediatePropagation();
160 159
     return false;
161  
-  }
  160
+  },
162 161
 
163  
-  function callFormSubmitBindings(form) {
  162
+  callFormSubmitBindings: function(form) {
164 163
     var events = form.data('events'), continuePropagation = true;
165 164
     if (events != undefined && events['submit'] != undefined) {
166 165
       $.each(events['submit'], function(i, obj){
@@ -169,58 +168,65 @@
169 168
     }
170 169
     return continuePropagation;
171 170
   }
  171
+  };
  172
+
  173
+	if ('ajaxPrefilter' in $) {
  174
+		$.ajaxPrefilter(function(options, originalOptions, xhr){ rails.CSRFProtection(xhr); });
  175
+	} else {
  176
+		$(document).ajaxSend(function(e, xhr){ rails.CSRFProtection(xhr); });
  177
+	}
172 178
 
173 179
 	$('a[data-confirm], a[data-method], a[data-remote]').live('click.rails', function(e) {
174 180
 		var link = $(this);
175  
-		if (!allowAction(link)) return stopEverything(e);
  181
+		if (!rails.allowAction(link)) return rails.stopEverything(e);
176 182
 
177 183
 		if (link.data('remote') != undefined) {
178  
-			handleRemote(link);
  184
+			rails.handleRemote(link);
179 185
 			return false;
180 186
 		} else if (link.data('method')) {
181  
-			handleMethod(link);
  187
+			rails.handleMethod(link);
182 188
 			return false;
183 189
 		}
184 190
 	});
185 191
 
186 192
 	$('form').live('submit.rails', function(e) {
187 193
 		var form = $(this), remote = form.data('remote') != undefined;
188  
-		if (!allowAction(form)) return stopEverything(e);
  194
+		if (!rails.allowAction(form)) return rails.stopEverything(e);
189 195
 
190 196
 		// skip other logic when required values are missing or file upload is present
191  
-		if (blankInputs(form, 'input[name][required]') && fire(form, 'ajax:aborted:required')) {
  197
+		if (rails.blankInputs(form, 'input[name][required]') && rails.fire(form, 'ajax:aborted:required')) {
192 198
 			return !remote;
193 199
 		}
194  
-		if (nonBlankInputs(form, 'input:file')) {
195  
-			return fire(form, 'ajax:aborted:file');
  200
+		if (rails.nonBlankInputs(form, 'input:file')) {
  201
+			return rails.fire(form, 'ajax:aborted:file');
196 202
 		}
197 203
 
198 204
     // If browser does not support submit bubbling, then this live-binding will be called before direct
199 205
     // bindings. Therefore, we should directly call any direct bindings before remotely submitting form.
200  
-    if (!$.support.submitBubbles && callFormSubmitBindings(form) == false) return stopEverything(e)
  206
+    if (!$.support.submitBubbles && rails.callFormSubmitBindings(form) == false) return rails.stopEverything(e)
201 207
 
202 208
 		if (remote) {
203  
-			handleRemote(form);
  209
+			rails.handleRemote(form);
204 210
 			return false;
205 211
 		} else {
206 212
 			// slight timeout so that the submit button gets properly serialized
207  
-			setTimeout(function(){ disableFormElements(form); }, 13);
  213
+			setTimeout(function(){ rails.disableFormElements(form); }, 13);
208 214
 		}
209 215
 	});
210 216
 
211 217
 	$('form input[type=submit], form input[type=image], form button[type=submit], form button:not([type])').live('click.rails', function() {
212 218
 		var button = $(this);
213  
-		if (!allowAction(button)) return stopEverything(e);
  219
+		if (!rails.allowAction(button)) return rails.stopEverything(e);
214 220
 		// register the pressed submit button
215 221
 		var name = button.attr('name'), data = name ? {name:name, value:button.val()} : null;
216 222
 		button.closest('form').data('ujs:submit-button', data);
217 223
 	});
218 224
 
219 225
 	$('form').live('ajax:beforeSend.rails', function(event) {
220  
-		if (this == event.target) disableFormElements($(this));
  226
+		if (this == event.target) rails.disableFormElements($(this));
221 227
 	});
222 228
 
223 229
 	$('form').live('ajax:complete.rails', function(event) {
224  
-		if (this == event.target) enableFormElements($(this));
  230
+		if (this == event.target) rails.enableFormElements($(this));
225 231
 	});
226 232
 })( jQuery );

0 notes on commit d591441

Please sign in to comment.
Something went wrong with that request. Please try again.