backbone-model-file-upload
Advanced tools
Comparing version 1.0.0 to 1.0.2
@@ -16,4 +16,4 @@ // Backbone.Model File Upload v1.0.0 | ||
if (typeof define === 'function' && define.amd) { | ||
define(['underscore', 'jquery', 'backbone'], function(_, $, Backbone){ | ||
factory(root, Backbone, _, $); | ||
define(['underscore', 'backbone'], function(_, Backbone){ | ||
factory(root, Backbone, _); | ||
}); | ||
@@ -23,11 +23,11 @@ | ||
} else if (typeof exports !== 'undefined') { | ||
var _ = require('underscore'), $ = require('jquery'), Backbone = require('backbone'); | ||
factory(root, Backbone, _, $); | ||
var _ = require('underscore'), Backbone = require('backbone'); | ||
factory(root, Backbone, _); | ||
// Browser global | ||
} else { | ||
factory(root, root.Backbone, root._, root.$); | ||
factory(root, root.Backbone, root._); | ||
} | ||
}(this, function(root, Backbone, _, $) { | ||
}(this, function(root, Backbone, _) { | ||
'use strict'; | ||
@@ -48,3 +48,4 @@ | ||
// Variables | ||
var attrs, attributes = this.attributes; | ||
var attrs, attributes = this.attributes, | ||
that = this; | ||
@@ -92,9 +93,10 @@ // Signature parsing - taken directly from original Backbone.Model.save | ||
_.each( formAttrs, function( value, key ){ | ||
if (value instanceof FileList) { | ||
if (value instanceof FileList || (key === that.fileAttribute && value instanceof Array)) { | ||
_.each(value, function(file) { | ||
formData.append( key, file ); | ||
}); | ||
return; | ||
} | ||
formData.append( key, value ); | ||
else { | ||
formData.append( key, value ); | ||
} | ||
}); | ||
@@ -107,8 +109,9 @@ | ||
// Apply custom XHR for processing status & listen to "progress" | ||
var that = this; | ||
options.xhr = function() { | ||
var xhr = $.ajaxSettings.xhr(); | ||
xhr.upload.addEventListener('progress', that._progressHandler.bind(that), false); | ||
return xhr; | ||
// Handle "progress" events | ||
if (!options.xhr) { | ||
options.xhr = function(){ | ||
var xhr = Backbone.$.ajaxSettings.xhr(); | ||
xhr.upload.addEventListener('progress', _.bind(that._progressHandler, that), false); | ||
return xhr | ||
} | ||
} | ||
@@ -115,0 +118,0 @@ } |
{ | ||
"name": "backbone-model-file-upload tests", | ||
"name": "backbone-model-file-upload", | ||
"description": "A concise, non-iframe, & pure XHR2/AJAX Backbone.model file upload. (Good for IE >= 10, FF, Chrome.)", | ||
"version": "1.0.0", | ||
"main": "backbone-model-file-upload.js", | ||
"keywords": [ | ||
"backbone", | ||
"fileupload", | ||
"file", | ||
"model", | ||
"multipart" | ||
], | ||
"author": "Joe Vu", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/homeslicesolutions/backbone-model-file-upload/issues" | ||
}, | ||
"homepage": "https://github.com/homeslicesolutions/backbone-model-file-upload", | ||
"dependencies": { | ||
"jquery": "~2.1.1", | ||
"jquery": "~2.1.1", | ||
"backbone": "~1.1.2", | ||
"Blob": "*" | ||
"Blob": "*" | ||
} | ||
} |
@@ -0,0 +0,0 @@ module.exports = function(grunt) { |
{ | ||
"name": "backbone-model-file-upload", | ||
"version": "1.0.0", | ||
"version": "1.0.2", | ||
"description": "A concise, non-iframe, & pure XHR2/AJAX Backbone.model file upload. (Good for IE >= 10, FF, Chrome.)", | ||
@@ -9,12 +9,3 @@ "main": "backbone-model-file-upload.js", | ||
}, | ||
"dependencies": { | ||
"bower": "^1.3.12", | ||
"formidable": "~1.0.15", | ||
"grunt": "^0.4.5", | ||
"grunt-contrib-jasmine": "^0.6.5", | ||
"grunt-run": "^0.3.0", | ||
"grunt-run-node": "^0.1.0", | ||
"grunt-template-jasmine-requirejs": "^0.2.0", | ||
"lodash": "~2.4.1" | ||
}, | ||
"dependencies": {}, | ||
"scripts": { | ||
@@ -40,3 +31,12 @@ "test": "grunt test" | ||
"homepage": "https://github.com/homeslicesolutions/backbone-model-file-upload", | ||
"devDependencies": {} | ||
"devDependencies": { | ||
"bower": "^1.3.12", | ||
"formidable": "~1.0.15", | ||
"grunt": "^0.4.5", | ||
"grunt-contrib-jasmine": "^0.6.5", | ||
"grunt-run": "^0.3.0", | ||
"grunt-run-node": "^0.1.0", | ||
"grunt-template-jasmine-requirejs": "^0.2.0", | ||
"lodash": "~2.4.1" | ||
} | ||
} |
Backbone.Model File Upload | ||
========================== | ||
[![Travis Build](https://travis-ci.org/homeslicesolutions/backbone-model-file-upload.svg?branch=master)](https://travis-ci.org/homeslicesolutions/backbone-model-file-upload) | ||
A concise, non-iframe, & pure XHR2/AJAX Backbone.model file upload. (Good for IE >= 10, FF, Chrome.) | ||
@@ -46,4 +44,4 @@ | ||
email.on('progress', console.log); | ||
// Will result: the status in percent i.e. 0.3233 | ||
email.on('progress', function(evt) { console.log(evt) }); | ||
// Will result: with an event object you can use evt.loaded and evt.total to figure out the percent (i.e. 0.3233) | ||
@@ -180,2 +178,5 @@ ``` | ||
#### Version 1.0.2 | ||
- Cutting a tag and fixing NPM issues | ||
### Dev/Installation | ||
@@ -182,0 +183,0 @@ If you want to work on this plugin, test it, etc., it just needs an install of `node` and `grunt`. |
@@ -21,2 +21,3 @@ !function(){ | ||
var simulatedFileObj; | ||
var simulatedFileObj2; | ||
@@ -26,2 +27,3 @@ beforeEach(function(){ | ||
simulatedFileObj = new Blob(['<strong>Hello World</strong>'], {type : 'text/html'}); | ||
simulatedFileObj2 = new Blob(['<strong>Hello Again, World</strong>'], {type : 'text/html'}); | ||
@@ -47,3 +49,3 @@ fileModel = new File({ | ||
it('should detect the file(blob) save successfully', function(done){ | ||
// Arrange | ||
@@ -54,3 +56,3 @@ fileModel.set({fileAttachment: simulatedFileObj}); | ||
fileModel.on('sync', function(model){ | ||
// Assert | ||
@@ -77,3 +79,3 @@ expect(model.get('from')).toBe('sample@email.com'); | ||
fileModel.on('sync', function(model){ | ||
// Assert | ||
@@ -100,3 +102,2 @@ expect(model.get('from')).toBe('sample@email.com'); | ||
fileModel.on('sync', function(model){ | ||
// Assert | ||
@@ -118,2 +119,24 @@ expect(model.get('from')).toBe('sample@email.com'); | ||
it ('should be able to save an array of multiple file objects', function(done){ | ||
// Listen | ||
fileModel.on('sync', function(model){ | ||
// Assert | ||
expect(model.get('from')).toBe('sample@email.com'); | ||
// Assert Blob (phantomJS can't do Blobs so just test minimal attributes) | ||
expect(model.get('fileAttachment')[0].size).toBe(28); | ||
expect(model.get('fileAttachment')[0].type).toBe('text/html'); | ||
//expect(model.get('fileAttachment')[0].data).toBe('data:text/html;base64,PHN0cm9uZz5IZWxsbyBXb3JsZDwvc3Ryb25nPg==') | ||
expect(model.get('fileAttachment')[1].size).toBe(35); | ||
expect(model.get('fileAttachment')[1].type).toBe('text/html'); | ||
//expect(model.get('fileAttachment')[1].data).toBe('data:text/html;base64,PHN0cm9uZz5IZWxsbyBBZ2FpbiwgV29ybGQ8L3N0cm9uZz4K=') | ||
done(); | ||
}); | ||
// Act | ||
fileModel.save('fileAttachment', [simulatedFileObj, simulatedFileObj2], {formData: true}); | ||
}); | ||
it ('should be able to have "wait" and "validate" option', function(done){ | ||
@@ -123,3 +146,3 @@ | ||
fileModel.on('sync', function(model){ | ||
// Assert | ||
@@ -145,3 +168,3 @@ expect(model.get('from')).toBe('sample@email.com'); | ||
fileModel.on('sync', function(model){ | ||
// Assert | ||
@@ -165,3 +188,3 @@ expect(model.get('from')).toBe('somethingelse@email.com'); | ||
fileModel.on('sync', function(model){ | ||
// Assert | ||
@@ -184,3 +207,3 @@ expect(model.get('from')).toBe('yes'); | ||
fileModel.on('sync', function(model){ | ||
// Assert | ||
@@ -205,3 +228,3 @@ expect(model.get('from')).toBe('yes'); | ||
fileModel.on('sync', function(model){ | ||
// Assert | ||
@@ -235,3 +258,3 @@ // Assert Blob (phantomJS can't do Blobs so just test minimal attributes) | ||
fileModel.on('sync', function(model){ | ||
// Assert | ||
@@ -284,3 +307,3 @@ expect(model.get('from')).toBe('yes'); | ||
console.log(fileModel._flatten({ | ||
console.log(JSON.stringify(fileModel._flatten({ | ||
'family': 'The Smiths', | ||
@@ -304,3 +327,3 @@ 'grandpa': { | ||
} | ||
})); | ||
}))); | ||
@@ -312,2 +335,2 @@ }); | ||
}(); | ||
}(); |
@@ -18,5 +18,13 @@ 'use strict'; | ||
var form = new formidable.IncomingForm(); | ||
var form = new formidable.IncomingForm(), | ||
files = {}, | ||
fields = {}; | ||
form.parse(req, function(err, fields, files) { | ||
form.on('file', function(field, file) { | ||
files[field] = (files[field] || []).concat([file]); | ||
}); | ||
form.on('field', function(field, value) { | ||
fields[field] = value; | ||
}); | ||
form.on('end', function(err) { | ||
console.log('POST Finished and publishing object back to browser...'); | ||
@@ -37,25 +45,20 @@ | ||
_.extend(output, unflatten(fields)); | ||
var fileOutputs = {}; | ||
_.each(files, function(files, filename){ | ||
var loopOutputs = []; | ||
_.each(files, function(file) { | ||
var fileOutput = {}, | ||
data = fs.readFileSync(file.path); | ||
fileOutput.type = file.type; | ||
fileOutput.size = data.length; | ||
fileOutput.name = file.name; | ||
fileOutput.lastModifiedDate = file.lastModifiedDate; | ||
fileOutput.data = 'data:' + file.type + ';base64,' + data.toString('base64'); | ||
loopOutputs.push(fileOutput); | ||
}); | ||
loopOutputs = loopOutputs.length > 1 ? loopOutputs : loopOutputs[0]; | ||
fileOutputs[filename] = loopOutputs; | ||
}); | ||
output = _.merge(output, fileOutputs); | ||
if (!_.isEmpty(files)) { | ||
for (var i in files) { | ||
if (files.hasOwnProperty(i)) { | ||
output[i] = {}; | ||
fs.readFile(files[i].path, function (err, data) { | ||
output[i].type = files[i].type; | ||
output[i].size = files[i].size; | ||
output[i].name = files[i].name; | ||
output[i].lastModifiedDate = files[i].lastModifiedDate; | ||
output[i].data = 'data:' + files[i].type + ';base64,' + data.toString('base64'); | ||
}); | ||
} | ||
} | ||
} | ||
console.info(output); | ||
@@ -67,2 +70,4 @@ | ||
form.parse(req) | ||
}); | ||
@@ -69,0 +74,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30206
0
543
193
8
- Removedbower@^1.3.12
- Removedformidable@~1.0.15
- Removedgrunt@^0.4.5
- Removedgrunt-contrib-jasmine@^0.6.5
- Removedgrunt-run@^0.3.0
- Removedgrunt-run-node@^0.1.0
- Removedlodash@~2.4.1
- Removedabbrev@1.1.1(transitive)
- Removedansi-regex@2.1.1(transitive)
- Removedansi-styles@1.0.02.2.1(transitive)
- Removedargparse@0.1.16(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@0.2.01.0.0(transitive)
- Removedasync@0.1.222.6.4(transitive)
- Removedaws-sign2@0.6.0(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedbl@1.0.3(transitive)
- Removedboom@2.10.1(transitive)
- Removedbower@1.8.14(transitive)
- Removedcaseless@0.11.0(transitive)
- Removedchalk@0.4.01.1.3(transitive)
- Removedcoffee-script@1.3.3(transitive)
- Removedcolors@0.6.2(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removedcommander@2.20.3(transitive)
- Removedconcat-stream@1.5.0(transitive)
- Removedcore-util-is@1.0.21.0.3(transitive)
- Removedcryptiles@2.0.5(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddateformat@1.0.2-1.2.3(transitive)
- Removeddebug@0.7.4(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedes5-shim@2.3.0(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedesprima@1.0.4(transitive)
- Removedeventemitter2@0.4.14(transitive)
- Removedexit@0.1.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedextract-zip@1.5.0(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfd-slicer@1.0.1(transitive)
- Removedfindup-sync@0.1.3(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@1.0.1(transitive)
- Removedformidable@1.0.17(transitive)
- Removedfs-extra@0.26.7(transitive)
- Removedgenerate-function@2.3.1(transitive)
- Removedgenerate-object-property@1.2.0(transitive)
- Removedgetobject@0.1.0(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedglob@3.1.213.2.11(transitive)
- Removedgraceful-fs@1.2.34.2.11(transitive)
- Removedgrunt@0.4.5(transitive)
- Removedgrunt-contrib-jasmine@0.6.5(transitive)
- Removedgrunt-legacy-log@0.1.3(transitive)
- Removedgrunt-legacy-log-utils@0.1.1(transitive)
- Removedgrunt-legacy-util@0.2.0(transitive)
- Removedgrunt-lib-phantomjs@0.4.0(transitive)
- Removedgrunt-run@0.3.0(transitive)
- Removedgrunt-run-node@0.1.7(transitive)
- Removedgrunt-template-jasmine-requirejs@0.2.3(transitive)
- Removedhar-validator@2.0.6(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removedhas-color@0.1.7(transitive)
- Removedhasha@2.2.0(transitive)
- Removedhawk@3.1.3(transitive)
- Removedhoek@2.16.3(transitive)
- Removedhooker@0.2.3(transitive)
- Removedhttp-signature@1.1.1(transitive)
- Removediconv-lite@0.2.11(transitive)
- Removedinherits@1.0.22.0.4(transitive)
- Removedis-my-ip-valid@1.0.1(transitive)
- Removedis-my-json-valid@2.20.6(transitive)
- Removedis-property@1.0.2(transitive)
- Removedis-stream@1.1.0(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisarray@1.0.0(transitive)
- Removedisexe@2.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjs-yaml@2.0.5(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsonfile@2.4.0(transitive)
- Removedjsonpointer@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedkew@0.7.0(transitive)
- Removedklaw@1.3.1(transitive)
- Removedlodash@0.9.22.4.24.17.21(transitive)
- Removedlru-cache@2.7.3(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedminimatch@0.2.140.3.0(transitive)
- Removedminimist@0.0.8(transitive)
- Removedmkdirp@0.5.0(transitive)
- Removednode-uuid@1.4.8(transitive)
- Removednopt@1.0.10(transitive)
- Removedoauth-sign@0.8.2(transitive)
- Removedpackage@1.0.1(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedpend@1.2.0(transitive)
- Removedphantomjs@1.9.20(transitive)
- Removedpinkie@2.0.4(transitive)
- Removedpinkie-promise@2.0.1(transitive)
- Removedprocess-nextick-args@1.0.7(transitive)
- Removedprogress@1.1.8(transitive)
- Removedqs@5.2.1(transitive)
- Removedreadable-stream@2.0.6(transitive)
- Removedrequest@2.67.0(transitive)
- Removedrequest-progress@2.0.1(transitive)
- Removedrimraf@2.1.42.2.8(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsemver@1.0.14(transitive)
- Removedsigmund@1.0.1(transitive)
- Removedsntp@1.0.9(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedstring_decoder@0.10.31(transitive)
- Removedstringstream@0.0.6(transitive)
- Removedstrip-ansi@0.1.13.0.1(transitive)
- Removedsupports-color@2.0.0(transitive)
- Removedtemporary@0.0.8(transitive)
- Removedthrottleit@1.0.1(transitive)
- Removedtough-cookie@2.2.2(transitive)
- Removedtunnel-agent@0.4.3(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removedtypedarray@0.0.7(transitive)
- Removedunderscore@1.7.0(transitive)
- Removedunderscore.string@2.2.12.3.32.4.0(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedverror@1.10.0(transitive)
- Removedwhich@1.0.91.2.14(transitive)
- Removedxtend@4.0.2(transitive)
- Removedyauzl@2.4.1(transitive)