Skip to content

Instantly share code, notes, and snippets.

@ninjabiscuit
Created March 22, 2014 10:27
Show Gist options
  • Save ninjabiscuit/9704570 to your computer and use it in GitHub Desktop.
Save ninjabiscuit/9704570 to your computer and use it in GitHub Desktop.
Sir Trevor Image block with better upload errorreporting
/*
Simple Image Block
*/
SirTrevor.Blocks.Image = SirTrevor.Block.extend({
type: "image",
title: function() { return i18n.t('blocks:image:title'); },
droppable: true,
uploadable: true,
icon_name: 'image',
loadData: function(data){
// Create our image tag
this.$editor.html($('<img>', { src: data.file.url }));
},
onBlockRender: function(){
/* Setup the upload button */
this.$inputs.find('button').bind('click', function(ev){ ev.preventDefault(); });
this.$inputs.find('input').on('change', _.bind(function(ev){
this.onDrop(ev.currentTarget);
}, this));
},
onUploadSuccess : function(data) {
this.setData(data);
this.ready();
},
onUploadError : function(error){
// here you should be able to access the responseText from the upload object.
// If you have returned JSON, parse it and get at the error value, then add it to the message
this.addMessage([i18n.t('blocks:image:upload_error'), JSON.parse(this.upload.responseText).error].join(' '));
this.ready();
},
onDrop: function(transferData){
var file = transferData.files[0],
urlAPI = (typeof URL !== "undefined") ? URL : (typeof webkitURL !== "undefined") ? webkitURL : null;
// Handle one upload at a time
if (/image/.test(file.type)) {
this.loading();
// Show this image on here
this.$inputs.hide();
this.$editor.html($('<img>', { src: urlAPI.createObjectURL(file) })).show();
this.upload = this.uploader(file, this.onUploadSuccess, this.onUploadError);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment