Refactored some code and updated read me

This commit is contained in:
homeslicesolutions 2014-09-11 03:21:24 -07:00
parent b049eede1f
commit dfef13aa7b
2 changed files with 16 additions and 13 deletions

View File

@ -19,6 +19,8 @@ model.save([file attribute], [file object], [options]);
In terms of how to use these methods, they have not changed. The only difference is that it has the capability to take a File object grabbed from the DOM (i.e. `<input type="file" />`). In terms of how to use these methods, they have not changed. The only difference is that it has the capability to take a File object grabbed from the DOM (i.e. `<input type="file" />`).
File, Blob, and FileList are all valid in the model. Once set it will be uploaded when ready.
As the file is being uploaded, a trigger `progress` fires as the browser sends chunks of data. The `progress` trigger sends a progress status in percents. As the file is being uploaded, a trigger `progress` fires as the browser sends chunks of data. The `progress` trigger sends a progress status in percents.
If you want to force not using FormData, add the option `{ formData: false }` and the whether or not you have a file object in the model, it'll try to send it as part of the JSON object. Opposite is true (for whichever circumstance) is that if you set `{ formData: true }`, it will force the usage of FormData. Not setting it will leave it automatic and it'll try to detect if there is the file in the model. If you want to force not using FormData, add the option `{ formData: false }` and the whether or not you have a file object in the model, it'll try to send it as part of the JSON object. Opposite is true (for whichever circumstance) is that if you set `{ formData: true }`, it will force the usage of FormData. Not setting it will leave it automatic and it'll try to detect if there is the file in the model.
@ -149,3 +151,6 @@ requirejs.config({
- Fixed all the wonkiness from 0.3 with the attributes and save. Reverted a lot of code and refactored - Fixed all the wonkiness from 0.3 with the attributes and save. Reverted a lot of code and refactored
- Added "blob" support as a FileObj entity - Added "blob" support as a FileObj entity
- Added some Jasmine tests - Added some Jasmine tests
## Version 0.5
- Added FileList support

View File

@ -1,10 +1,11 @@
// Backbone.Model File Upload v0.4 // Backbone.Model File Upload v0.5
// by Joe Vu - joe.vu@homeslicesolutions.com // by Joe Vu - joe.vu@homeslicesolutions.com
// 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
// Contributors: // Contributors:
// lutherism - Alex Jansen - alex.openrobot.net // lutherism - Alex Jansen - alex.openrobot.net
// bildja - Dima Bildin - github.com/bildja // bildja - Dima Bildin - github.com/bildja
// Minjung - Alejandro - github.com/Minjung
(function (root, factory) { (function (root, factory) {
if (typeof define === 'function' && define.amd) { if (typeof define === 'function' && define.amd) {
@ -87,12 +88,9 @@
options.contentType = false; options.contentType = false;
// Apply custom XHR for processing status & listen to "progress" // Apply custom XHR for processing status & listen to "progress"
var that = this;
options.xhr = function() { options.xhr = function() {
var xhr = $.ajaxSettings.xhr(); var xhr = $.ajaxSettings.xhr();
xhr.upload.addEventListener('progress', function(){ xhr.upload.addEventListener('progress', this._progressHandler.bind(this), false);
that._progressHandler.apply(that, arguments);
}, false);
return xhr; return xhr;
} }
} }