var MediaUpload = UploadView.extend({ el: "#fileUpload", createAction: "/api/media/new", uploadAction: "/api/media/upload", events: { 'mousedown': "stopPropagation", "keydown .url": "enterSubmit", }, initialize: function(opt){ this.__super__.initialize.call(this) this.parent = opt.parent this.$url = this.$(".url") }, show: function(){ this.$el.addClass("active") this.$url.val("") }, hide: function(){ this.$el.removeClass("active") }, enterSubmit: function(e){ e.stopPropagation() if (e.keyCode == 13) { e.preventDefault() this.parse() } }, parse: function(){ var url = this.$url.val() this.$url.val("") this.parseUrl(url) }, parseUrl: function(url){ Parser.parse(url, function(media){ if (! media) { alert("Not a valid image/video link") return } media._csrf = $("[name=_csrf]").val() var request = $.ajax({ type: "post", url: this.createAction, data: media, }) request.done(this.add.bind(this)) }.bind(this)) }, add: function(media){ console.log(media) this.parent.mediaViewer.addUploadedMedia(media) }, error: function(error){ console.log(error) alert(error.errors.media.message) }, beforeUpload: function(){ this.parent.mediaViewer.deleteArmed(false) } })