Added UMD approach to the module

This commit is contained in:
Dima Bildin 2014-07-26 18:40:32 +03:00
parent 88c3f2d1a7
commit c8360385e7

View File

@ -3,13 +3,21 @@
// For all details and documentation: // For all details and documentation:
// https://github.com/homeslicesolutions/backbone-model-file-upload // https://github.com/homeslicesolutions/backbone-model-file-upload
!function(_, Backbone){ (function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['underscore', 'backbone'], factory);
} else {
// Browser globals
factory(_, Backbone);
}
}(this, function(_, Backbone){
// Clone the original Backbone.Model.prototype // Clone the original Backbone.Model.prototype
var backboneModelClone = _.clone( Backbone.Model.prototype ); var backboneModelClone = _.clone( Backbone.Model.prototype );
// Extending out // Extending out
_.extend(Backbone.Model.prototype, { _.extend(Backbone.Model.prototype, {
// ! Default file attribute - can be overwritten // ! Default file attribute - can be overwritten
fileAttribute: 'file', fileAttribute: 'file',
@ -20,7 +28,7 @@
// Variables // Variables
var attrs, attributes = this.attributes; var attrs, attributes = this.attributes;
// Signature parsing - taken directly from original Backbone.Model.save // Signature parsing - taken directly from original Backbone.Model.save
// and it states: 'Handle both "key", value and {key: value} -style arguments.' // and it states: 'Handle both "key", value and {key: value} -style arguments.'
if (key == null || typeof key === 'object') { if (key == null || typeof key === 'object') {
attrs = key; attrs = key;
@ -41,11 +49,11 @@
} }
// Check for "formData" flag and check for if file exist. // Check for "formData" flag and check for if file exist.
if ( options.formData === true if ( options.formData === true
|| options.formData !== false || options.formData !== false
&& this.attributes[ this.fileAttribute ] && this.attributes[ this.fileAttribute ]
&& this.attributes[ this.fileAttribute ] instanceof File ) { && this.attributes[ this.fileAttribute ] instanceof File ) {
// Flatten Attributes reapplying File Object // Flatten Attributes reapplying File Object
var formAttrs = _.clone( this.attributes ), var formAttrs = _.clone( this.attributes ),
fileAttr = this.attributes[ this.fileAttribute ]; fileAttr = this.attributes[ this.fileAttribute ];
@ -73,7 +81,7 @@
that._progressHandler.apply(that, arguments); that._progressHandler.apply(that, arguments);
}, false); }, false);
return xhr; return xhr;
} }
} }
// Resume back to original state // Resume back to original state
@ -81,7 +89,7 @@
// Continue to call the existing "save" method // Continue to call the existing "save" method
return backboneModelClone.save.call(this, attrs, options); return backboneModelClone.save.call(this, attrs, options);
}, },
// _ FlattenObject gist by "penguinboy". Thank You! // _ FlattenObject gist by "penguinboy". Thank You!
@ -103,7 +111,7 @@
return output; return output;
}, },
// _ Get the Progress of the uploading file // _ Get the Progress of the uploading file
_progressHandler: function( event ) { _progressHandler: function( event ) {
if (event.lengthComputable) { if (event.lengthComputable) {
@ -114,4 +122,4 @@
}); });
}(_, Backbone); }));