Skip to content

Instantly share code, notes, and snippets.

@abuiles
Created October 23, 2014 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abuiles/ae56c48e26c1ed103a0e to your computer and use it in GitHub Desktop.
Save abuiles/ae56c48e26c1ed103a0e to your computer and use it in GitHub Desktop.
import Ember from 'ember';
import DS from 'ember-data';
import { request } from 'ic-ajax';
export default Ember.Mixin.create({
attachment: null,
saveWithAttachment: function() {
return this.createWithAttachment();
},
createWithAttachment: function() {
var adapter, attachmentKey, data, formData, promise, root, serializer, url,
_this = this;
adapter = this.store.adapterFor(this.constructor);
serializer = this.store.serializerFor(this.constructor.typeKey);
formData = new FormData();
attachmentKey = this.get('attachment');
data = Ember.copy(this.serialize());
data[attachmentKey] = this.get(attachmentKey);
formData = new FormData();
root = this._rootKey();
Ember.keys(data).forEach(function(key) {
if (key === attachmentKey) {
return formData.append("" + root + "[" + key + "]", data[key]);
} else {
if (!Ember.isEmpty(data[key])) {
if (Ember.isArray(data[key])) {
return data[key].forEach(function(val) {
return formData.append("" + root + "[" + key + "][]", val);
});
} else {
return formData.append("" + root + "[" + key + "]", data[key]);
}
}
}
});
url = adapter.buildURL(this.constructor.typeKey, this.get('id'));
this.adapterWillCommit();
promise = request(url, {
type: this._requestType(),
data: formData,
dataType: 'json',
processData: false,
contentType: false,
xhr: function() {
var xhr;
xhr = Ember.$.ajaxSettings.xhr();
xhr.upload.onprogress = function(evt) {
return _this.set('uploadProgress', evt.loaded / evt.total * 100);
};
return xhr;
}
});
return this._commitWithAttachment(promise, adapter, serializer);
},
_rootKey: function() {
return Ember.String.underscore(Ember.String.decamelize(this.constructor.typeKey));
},
_requestType: function() {
if (this.get("isNew")) {
return "POST";
} else {
return "PUT";
}
},
_commitWithAttachment: function(promise, adapter, serializer) {
var operation, record, store, type;
store = this.store;
record = this;
type = record.constructor;
operation = '';
if (Ember.get(record, "isNew")) {
operation = "createRecord";
} else if (Ember.get(record, "isDeleted")) {
operation = "deleteRecord";
} else {
operation = "updateRecord";
}
return promise.then((function(adapterPayload) {
var payload;
payload = void 0;
if (adapterPayload) {
payload = serializer.extract(store, type, adapterPayload, Ember.get(record, "id"), operation);
} else {
payload = adapterPayload;
}
store.didSaveRecord(record, payload);
return record;
}), (function(reason) {
if (reason instanceof DS.InvalidError) {
store.recordWasInvalid(record, reason.errors);
} else {
store.recordWasError(record, reason);
}
throw reason;
}), "Uploading file with attachment");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment