Sha256: 18b9b021913c67eba643b798b11c5149362f6834869b9508be7006800e386acc

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

jQuery(function ($) {
    $.fn.extend({
		/**
	     * Handles execution of remote calls involving file uploads, firing overridable events along the way
	     */
	    callRemotipart: function () {
	        var el      = this,
	            url     = el.attr('action');

	        if (url === undefined) {
	          throw "No URL specified for remote call (action must be present).";
	        } else {
	            if (el.triggerAndReturn('ajax:before')) {
					if(url.substr(-3) != '.js') url += '.js'; //force rails to respond to respond to the request with :format = js
	                el.ajaxSubmit({
	                    url: url,
	                    dataType: 'script',
	                    beforeSend: function (xhr) {
	                        el.trigger('ajax:loading', xhr);
	                    },
	                    success: function (data, status, xhr) {
							console.log(data);
	                        el.trigger('ajax:success', [data, status, xhr]);
	                    },
	                    complete: function (xhr) {
	                        el.trigger('ajax:complete', xhr);
	                    },
	                    error: function (xhr, status, error) {
	                        el.trigger('ajax:failure', [xhr, status, error]);
	                    }
	                });
	            }

	            el.trigger('ajax:after');
	        }
		},
		_callRemote: $.fn.callRemote, //store the original rails callRemote
		callRemote: function(){ //override the rails callRemote and check for a file input
			if(this.find('input:file').length){
				this.callRemotipart();
			} else {
				this._callRemote();
			}
		}
	});

});

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
remotipart-0.1.0 lib/generators/templates/jquery.remotipart.js