var MediaViewer = ModalView.extend({ el: ".mediaDrawer.mediaViewer", createAction: "/api/docs/new", updateAction: "/api/docs/edit", destroyAction: "/api/docs/destroy", show: function(){ if (! this.loaded) { this.load() } else { this.__super__.show.call(this) } }, load: function(){ $.get("/api/media/user", $.proxy(this.populate, this)) }, populate: function(data){ data.forEach($.proxy(this.add, this)) this.__super__.show.call(this) }, add: function(media){ var image = new Image () var $span = $("") $span.addClass("mediaContainer") switch (media.type) { case 'image': image.src = media.url break case 'youtube': case 'vimeo': image.src = media.thumbnail break } $span.data("media", media) $span.append(image) this.$(".myMedia").prepend($span) }, destroy: function(name, cb){ $.ajax({ type: "delete", url: this.destroyAction, data: { name: name, _csrf: $("[name=_csrf]").val() } }).complete(cb || function(){}) }, })