From 3d9da19009fa45366e04a90824d0f28cd1c4fbad Mon Sep 17 00:00:00 2001 From: Josiah Baldwin Date: Tue, 19 Jan 2016 11:36:07 -0800 Subject: [PATCH] Updated file upload array to be more simple. Fixed problem when using older browsers that do not support function.prototype.bind (or phantomJS in this case, for the tests) --- backbone-model-file-upload.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/backbone-model-file-upload.js b/backbone-model-file-upload.js index 946fc0c..c0f7c90 100644 --- a/backbone-model-file-upload.js +++ b/backbone-model-file-upload.js @@ -43,7 +43,8 @@ save: function(key, val, options) { // Variables - var attrs, attributes = this.attributes; + var attrs, attributes = this.attributes, + that = this; // Signature parsing - taken directly from original Backbone.Model.save // and it states: 'Handle both "key", value and {key: value} -style arguments.' @@ -86,12 +87,7 @@ // Converting Attributes to Form Data var formData = new FormData(); _.each( formAttrs, function( value, key ){ - if (value instanceof FileList) { - _.each(value, function(file) { - formData.append( key, file ); - }); - } - else if (value instanceof Array && value.length && value[0] instanceof File) { + if (value instanceof FileList || (key === that.fileAttribute && value instanceof Array)) { _.each(value, function(file) { formData.append( key, file ); }); @@ -107,11 +103,10 @@ options.contentType = false; // Handle "progress" events - var that = this; if (!options.xhr) { options.xhr = function(){ var xhr = Backbone.$.ajaxSettings.xhr(); - xhr.upload.addEventListener('progress', that._progressHandler.bind(that), false); + xhr.upload.addEventListener('progress', _.bind(that._progressHandler, that), false); return xhr } }