remotipart 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,15 +17,17 @@ This gem augments the native Rails jQuery remote form function enabling asynchro
17
17
  +---------------------------------------------------------------------+
18
18
  | Version of remotipart.js | is compatible with versions of rails.js* |
19
19
  +---------------------------------------------------------------------+
20
- | <= 0.3.1 | <= 498b35e24cdb14f2 |
20
+ | <= 0.3.1 | <= 498b35e24cdb14f2 |
21
21
  | 0.3.2 | > 498b35e24cdb14f2, <= 7f2acc1811f62877 |
22
22
  | 0.3.3 | > 7f2acc1811f62877, <= d2abd6f9df4e4a42 |
23
+ | 0.3.4 | > d2abd6f9df4e4a42 (i.e. latest) |
23
24
  +---------------------------------------------------------------------+
24
25
 
25
26
  *rails.js versions <em>(For a full list of Rails.js]versions and diffs, see {Rails jQuery UJS Changelog}[https://github.com/rails/jquery-ujs/blob/master/CHANGELOG.md])</em>:
26
27
  * {498b35e24cdb14f2}[https://github.com/rails/jquery-ujs/raw/498b35e24cdb14f2d94486e8a1f4a1f661091426/src/rails.js]
27
28
  * {7f2acc1811f62877}[https://github.com/rails/jquery-ujs/raw/7f2acc1811f62877611e16451530728b5e13dbe7/src/rails.js]
28
29
  * {d2abd6f9df4e4a42}[https://github.com/rails/jquery-ujs/raw/d2abd6f9df4e4a426c17c218b7d5e05004c768d0/src/rails.js]
30
+ * {latest}[https://github.com/rails/jquery-ujs/raw/master/src/rails.js]
29
31
 
30
32
  == Installation
31
33
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.3
1
+ 0.3.4
@@ -1,80 +1,84 @@
1
- (function ($) {
2
- // Triggers an event on an element and returns the event result
3
- function fire(obj, name, data) {
4
- var event = new $.Event(name);
5
- obj.trigger(event, data);
6
- return event.result !== false;
7
- }
8
-
9
- // Submits "remote" forms and links with ajax
10
- function handleRemote(element) {
11
- var method, url, data,
12
- dataType = element.attr('data-type') || ($.ajaxSettings && $.ajaxSettings.dataType) || 'script';
13
-
14
- // Since iframe-submitted form is submitted normal-style and cannot set custom headers,
15
- // we'll add a custom hidden input to keep track and let the server know this was still
16
- // an AJAX form.
17
- element.append($('<input />', {
18
- type: "hidden",
19
- name: "remotipart_submitted",
20
- value: true
21
- }))
22
- .data('remotipartSubmitted', dataType);
23
-
24
- if (fire(element, 'ajax:before')) {
25
- method = element.attr('method');
26
- url = element.attr('action');
27
- data = element.serializeArray();
28
- // memoized value from clicked submit button
29
- var button = element.data('ujs:submit-button');
30
- if (button) {
31
- data.push(button);
32
- element.data('ujs:submit-button', null);
33
- }
34
-
35
-
36
- if (dataType == 'script') {
37
- url = url.split('?'); // split on GET params
38
- if(url[0].substr(-3) != '.js') url[0] += '.js'; // force rails to respond to respond to the request with :format = js
39
- url = url.join('?'); // join on GET params
40
- }
41
- element.ajaxSubmit({
42
- url: url, type: method || 'GET', data: data, dataType: dataType,
43
- // stopping the "ajax:beforeSend" event will cancel the ajax request
44
- beforeSend: function(xhr, settings) {
45
- return fire(element, 'ajax:beforeSend', [xhr, settings]);
46
- },
47
- success: function(data, status, xhr) {
48
- element.trigger('ajax:success', [data, status, xhr]);
49
- },
50
- complete: function(xhr, status) {
51
- element.trigger('ajax:complete', [xhr, status]);
52
- },
53
- error: function(xhr, status, error) {
54
- element.trigger('ajax:error', [xhr, status, error]);
55
- }
56
- });
57
- }
58
- }
59
-
60
- function disableFormElements(form) {
61
- form.find('input[data-disable-with]').each(function() {
62
- var input = $(this);
63
- input.data('ujs:enable-with', input.val())
64
- .val(input.attr('data-disable-with'))
65
- .attr('disabled', 'disabled');
66
- });
67
- }
68
-
69
- $('form').live('ajax:aborted:file.remotipart', function(){
70
- var form = $(this), remote = form.attr('data-remote') != undefined;
71
-
72
- if (remote) {
73
- handleRemote(form);
74
- return false;
75
- } else {
76
- disableFormElements(form);
1
+ (function($) {
2
+
3
+ var remotipart;
4
+
5
+ $.remotipart = remotipart = {
6
+
7
+ setup: function(form) {
8
+ form
9
+ // Append hidden input so we can tell from back-end when form has been submitted by remotipart
10
+ .append($('<input />', {
11
+ 'id': "remotipart_submitted",
12
+ type: "hidden",
13
+ name: "remotipart_submitted",
14
+ value: true
15
+ }))
16
+
17
+ // Allow setup part of $.rails.handleRemot to setup remote settings before canceling default remote handler
18
+ .bind('ajax:beforeSend.remotipart', function(e, xhr, origSettings){
19
+ // Shallow copy
20
+ var settings = $.extend({}, origSettings);
21
+
22
+ // Delete the beforeSend bindings, since we're about to re-submit via ajaxSubmit with the beforeSubmit
23
+ // hook that was just setup and triggered via the default `$.rails.handleRemote`
24
+ delete settings.beforeSend;
25
+ // form.js's `ajaxSubmit` will re-create this for us
26
+ delete settings.data;
27
+
28
+ // Manually set the default dataType if not defined, since the form.js's `ajaxSubmit` function
29
+ // does not actually submit an xhr request (which has its default dataType set by rails.js)
30
+ if (settings.dataType === undefined) settings.dataType = 'script';
31
+
32
+ // Allow remotipartSubmit to be cancelled if needed
33
+ if ($.rails.fire(form, 'ajax:remotipartSubmit', [xhr, settings])) {
34
+
35
+ // Setup request URL for js dataType (needed for some versions of IE)
36
+ if (settings.dataType == 'script') {
37
+ settings.url = settings.url.split('?'); // split on GET params
38
+ if (settings.url[0].substr(-3) != '.js') settings.url[0] += '.js'; // force rails to respond to respond to the request with :format = js
39
+ settings.url = settings.url.join('?'); // join on GET params
40
+ }
41
+
42
+ // Update remotipartSubmitted data with dataType, in case needed in other scripts
43
+ form.data('remotipartSubmitted', settings.dataType);
44
+ // And finally, Use form.js's `ajaxSubmit` to do remote file uploading via iframe method
45
+ form.ajaxSubmit(settings);
46
+
47
+ // Cancel the default jquery-ujs remote call by returning false for `ajax:beforeSend`
48
+ return false;
49
+ }
50
+ })
51
+
52
+ // Keep track that we just set this particular form with Remotipart bindings
53
+ // Note: The `true` value will get over-written with the `settings.dataType` from the `ajax:beforeSend` handler
54
+ .data('remotipartSubmitted', true);
55
+ },
56
+
57
+ teardown: function(form) {
58
+ form
59
+ .unbind('ajax:beforeSend.remotipart')
60
+ .children('#remotipart_submitted').remove();
61
+ delete form.data.remotipartSubmitted;
77
62
  }
63
+ };
64
+
65
+ $('form').live('ajax:aborted:file', function(){
66
+ var form = $(this);
67
+
68
+ // Only need to setup form and make form bindings once.
69
+ // If form has already been setup, just let bindings be executed.
70
+ if (form.data('remotipartSubmitted') === undefined) remotipart.setup(form);
71
+
72
+ // If browser does not support submit bubbling, then this live-binding will be called before direct
73
+ // bindings. Therefore, we should directly call any direct bindings before remotely submitting form.
74
+ if (!$.support.submitBubbles && 'callFormSubmitBindings' in $.rails && $.rails.callFormSubmitBindings(form) === false) return $.rails.stopEverything(e);
75
+
76
+ // Manually call jquery-ujs remote call so that it can setup form and settings as usual,
77
+ // and trigger the `ajax:beforeSend` callback to which remotipart binds functionality.
78
+ $.rails.handleRemote(form);
79
+
80
+ // Return false to prevent standard browser behavior (part of jquery-ujs api for `ajax:aborted:file`)
81
+ return false;
78
82
  });
79
83
 
80
84
  })(jQuery);
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{remotipart}
8
- s.version = "0.3.3"
8
+ s.version = "0.3.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Greg Leppert", "Steve Schwartz"]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: remotipart
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 3
10
- version: 0.3.3
9
+ - 4
10
+ version: 0.3.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Greg Leppert