Skip to content

Instantly share code, notes, and snippets.

@zenlor
Created June 8, 2012 14:32
Show Gist options
  • Select an option

  • Save zenlor/2895919 to your computer and use it in GitHub Desktop.

Select an option

Save zenlor/2895919 to your computer and use it in GitHub Desktop.
inplace editing with ender
Admin.controllers :photos do
post :rename do
if params[:gallery]
Photo.set({:gallery => params[:gallery]}, {:gallery => params[:name], :gallery_slug => params[:name].to_slug})
{name: params[:name]}.to_json
else
403
end
end
end
$.fn.inEdit = function (target, options) {
var el = $(this);
target = $(target);
el.bind("click", function(e){
e.preventDefault();
target.attr('contenteditable', 'true')
.focus()
.bind('keydown', function(e){
if (e.keyCode != 13) return;
$(this).blur();
e.preventDefault();
target.attr('contenteditable', false);
var data = options.data || {};
data[options.name || 'name'] = target.text();
$.ajax({
url: options.url
, method: options.method
, type: 'json'
, data: data
, success: options.success || function(){}
, error: options.error || function(e) {console.log(e)}
});
});
// window.getSelection().setPosition(0);
});
}
$.domReady(function(){
$('a.edit.sym').each(function(el){
var el = $(el)
, target = el.parent().find('span');
el.inEdit(
target
, {
url:'/photos/rename'
, name: 'name'
, data: {gallery: target.text()}
, method:'post'
, success: function () { ui.notify('Photo', 'galleria rinominata').effect('slide'); }
, error: function () { ui.error('Photo', 'galleria rinominata').effect('slide'); }
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment