diff options
Diffstat (limited to 'public/assets/javascripts/ui/editor/MediaUpload.js')
| -rw-r--r-- | public/assets/javascripts/ui/editor/MediaUpload.js | 46 |
1 files changed, 25 insertions, 21 deletions
diff --git a/public/assets/javascripts/ui/editor/MediaUpload.js b/public/assets/javascripts/ui/editor/MediaUpload.js index 76bf6f0..a66f2e8 100644 --- a/public/assets/javascripts/ui/editor/MediaUpload.js +++ b/public/assets/javascripts/ui/editor/MediaUpload.js @@ -2,16 +2,18 @@ var MediaUpload = View.extend({ el: ".fileUpload", - createAction: "/api/media/url", + createAction: "/api/media/new", uploadAction: "/api/media/upload", events: { "keydown .url": "enterSubmit", "change .file": "handleFileSelect", + "submit form": "preventDefault", }, initialize: function(opt){ this.parent = opt.parent + this.$csrf = this.$("[name=_csrf]") this.$url = this.$(".url") this.$file = this.$(".file") this.$upload = this.$(".upload-icon") @@ -43,14 +45,15 @@ var MediaUpload = View.extend({ return } + media._csrf = this.$csrf.val() console.log(media) - $.ajax({ + var request = $.ajax({ type: "post", url: this.createAction, - data: rec, - success: $.proxy(this.add, this) + data: media, }) + request.done($.proxy(this.add, this)) }, this)) }, @@ -67,28 +70,29 @@ var MediaUpload = View.extend({ this.$upload.addClass('uploading') - var xhr = new XMLHttpRequest(), - fd = new FormData(); + var fd = new FormData(); + fd.append( 'image', f ) + fd.append( '_csrf', this.$csrf.val() ) - fd.append( 'file', f ); - - xhr.addEventListener("error", $.proxy(function(){ - console.log("error uploading file..") - this.$upload.removeClass('uploading') - }, this), false); - - xhr.onreadystatechange = $.proxy(function() { - if (xhr.readyState == 4 && xhr.status == 200) { - this.$upload.removeClass('uploading') - } - }, this) - - xhr.open("POST", '/api/media/upload', true); - xhr.send( fd ); + var request = $.ajax({ + url: this.uploadAction, + type: "post", + data: fd, + dataType: "json", + processData: false, + contentType: false, + }) + request.done($.proxy(this.add, this)) } }, add: function(media){ + console.log(media) + if (media.error) { + return + } + this.$upload.removeClass('uploading') + this.parent.mediaViewer.add(media) } }) |
