Added "attrs" fix and new just "options" feature

This commit is contained in:
Joe Vu 2014-08-06 17:46:18 -07:00
parent ac753be726
commit e46cc04f02

View File

@ -1,7 +1,10 @@
// Backbone.Model File Upload v0.1
// Backbone.Model File Upload v0.3
// by Joe Vu - joe.vu@homeslicesolutions.com
// For all details and documentation:
// https://github.com/homeslicesolutions/backbone-model-file-upload
// Contributors:
// lutherism - Alex Jansen - alex.openrobot.net
// bildja - Dima Bildin - github.com/bildja
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
@ -28,11 +31,16 @@
// Variables
var attrs, attributes = this.attributes;
// Signature parsing - taken directly from original Backbone.Model.save
// and it states: 'Handle both "key", value and {key: value} -style arguments.'
if (key == null || typeof key === 'object') {
// Signature parsing - similar to original Backbone.Model.save
// Handle (key, value, options), ({key: value}, options), and (options) -style arguments.
if (typeof key === 'object') {
if (typeof val === void 0) {
attrs = attributes;
options = key;
} else {
attrs = key;
options = val;
}
} else {
(attrs = {})[key] = val;
}
@ -51,12 +59,12 @@
// Check for "formData" flag and check for if file exist.
if ( options.formData === true
|| options.formData !== false
&& this.attributes[ this.fileAttribute ]
&& this.attributes[ this.fileAttribute ] instanceof File ) {
&& attrs[ this.fileAttribute ]
&& attrs[ this.fileAttribute ] instanceof File ) {
// Flatten Attributes reapplying File Object
var formAttrs = _.clone( this.attributes ),
fileAttr = this.attributes[ this.fileAttribute ];
var formAttrs = _.clone( attrs ),
fileAttr = attrs[ this.fileAttribute ];
formAttrs = this._flatten( formAttrs );
formAttrs[ this.fileAttribute ] = fileAttr;