Skip to content

Instantly share code, notes, and snippets.

@pgaertig
Created April 12, 2012 10:27
Show Gist options
  • Save pgaertig/2366316 to your computer and use it in GitHub Desktop.
Save pgaertig/2366316 to your computer and use it in GitHub Desktop.
JsonCommand
def destroy
#...
flash[:success] = t("entity_name.delete.success", :name => entity.name)
respond_to do |format|
format.html do
redirect_to :action=>:index
end
format.json { render :json => {
'delete-row'=>dom_id(entity)),
'flash-success' => flash.discard[:success]} }
end
end
JsonCommand = new function() {
var self = this;
function exec(data) {
for(var prop in data) {
if(data.hasOwnProperty(prop)) {
if(self.hasOwnProperty(prop)) {
self[prop](data[prop]);
} else {
alert('noop');
}
}
}
}
this.exec = exec;
}
JsonCommand['delete-row'] = function(data) {
$('#'+data).fadeOut();
}
JsonCommand['flash-success'] = function(data) {
$.blockUI({
message: data,
fadeIn: 700,
fadeOut: 700,
timeout: 2000,
showOverlay: false,
centerY: false,
css: {
width: '350px',
top: '10px',
left: '',
right: '10px',
border: 'none',
padding: '5px',
backgroundColor: 'green',
'-webkit-border-radius': '10px',
'-moz-border-radius': '10px',
opacity: .6,
color: '#fff'
}
});
}
$(function() {
$('.json-command').closest('form').attr('data-type','json').
bind('ajax:success',function(xhr,data){
JsonCommand.exec(data);
}).
bind('ajax:error', function(xhr,status, error){
alert("Operation failed: "+error)
});
$('.toggler').live('click',function(ev) {
$('#'+$(this).attr('id')+'-toggled').toggle();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment